Saturday 22 December 2018

c# - javascript validation for email format accepting incorrect email format @abcd.com.gmail,





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

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...