Thursday, June 14, 2012

Javascript - email address validation

Here's a simple way to validate email addresses. I prefer to set this on the onblur event.


<input type="text" id="tb_email" onblur="email_validation(this)">

function email_validation(input_txt) {
    if (input_txt.value == '')
        return true;
    var rex = /^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$/;
    if (rex.test(input_txt.value)) {
        return true;
    }
    alert('Please provide a valid email address');
    input_txt.focus();
    input_txt.select();
    return false;
}



Cheers!

No comments:

Post a Comment