Possible Duplicate:
Validate email address in Javascript?
I have to do validation for Email format, I am using the CODE below line to restrict Special Characters.
onkeypress="return AlphaNumericBox(event,this,'@._);"
But now the Problem is I don have any proper validation for the exact format for example its also accepting text like " @abcd.com.gmail,.. " Is there any javascript validation for this? 
any idea?
Thanks in advance.
Answer
function CheckEmail(address){
    address=address.replace(/(^\s*)|(\s*$)/g, "");
    var reg=/([\w._-])+@([\w_-])+(\.([\w_-])+){1,2}/;
    var matcharr=reg.exec(address);
    if(matcharr!=null){
        if(matcharr[0].length==address.length){
            return true;
        }
        return false;
    }
    return false;
}
eg:
var sVal=" t.st@gmail.com.cn ";
var sVal2=" t.st@gmail.com.cn.abc ";
console.log(CheckEmail("name@server.com"));    //outpus true
console.log(CheckEmail("@server.com"));   //outpus false
 
No comments:
Post a Comment