Archive for : September, 2014

Regex to match email addresses

The “official” fool-proof regex to match email addresses known as RFC 5322:


(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*
| "(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]
| \\[\x01-\x09\x0b\x0c\x0e-\x7f])*")
@ (?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?
| \[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}
(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:
(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]
| \\[\x01-\x09\x0b\x0c\x0e-\x7f])+)
\])

A more practical implementation of RFC 5322 if we omit the syntax using double quotes and square brackets. It will still match 99.99% of all email addresses in actual use today:


[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?

Read full article at: How to Find or Validate an Email Address

Javascript simple clock

Create a simple function that get the client time (hours:minutes:seconds) and prints it in the div:


<script type="text/javascript">
function getTime() {
var currentDate = new Date();
var hour = currentDate.getHours();
var min = currentDate.getMinutes();
var sec = currentDate.getSeconds();
var currentTime = hour + ":" + min + ":" + sec;
document.getElementById('clock').innerHTML = currentTime;
}
setInterval("getTime()", 1000);
</script>
<div id="clock"></div>

Add a custom right-click function to a web application

Add a custom right-click function to a web application. Also can be used to implemen a custom menu opened on right-click:


<script language="javascript" type="text/javascript">
document.oncontextmenu = RightMouseDown;
document.onmousedown = mouseDown;
function mouseDown(e) {
if (e.which == 3) {//righClick
console.log("Right-click goes here");
}
}
function RightMouseDown() {
console.log("Right-click Context goes here");
return false;
}
</script>