javascript - how to validate email -
this question has answer here:
- how validate email address in javascript? 65 answers
i need add add following characteristics form i'm stuck. b appreciated.
- one or more word characters
- exactly 1 at-sign
- one or more word characters
exactly 1 period 2 or more characters a-z, a-z, 0-9, period, or hyphen
<!doctype html> <html> <head> <script> function validateform() { var x=document.forms["myform"]["email"].value; var atpos=x.indexof("@"); var dotpos=x.lastindexof("."); if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length) { alert("not valid e-mail address"); return false; } } </script> </head> <body> <form name="myform" action="demo_form.asp" onsubmit="return validateform();" method="post"> email: <input type="text" name="email"> <input type="submit" value="submit"> </form> </body>
function isvalidemailaddress(emailaddress) { var pattern = new regexp(/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00a0-\ud7ff\uf900-\ufdcf\ufdf0-\uffef])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00a0-\ud7ff\uf900-\ufdcf\ufdf0-\uffef])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00a0-\ud7ff\uf900-\ufdcf\ufdf0-\uffef])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00a0-\ud7ff\uf900-\ufdcf\ufdf0-\uffef]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00a0-\ud7ff\uf900-\ufdcf\ufdf0-\uffef])|(([a-z]|\d|[\u00a0-\ud7ff\uf900-\ufdcf\ufdf0-\uffef])([a-z]|\d|-|\.|_|~|[\u00a0-\ud7ff\uf900-\ufdcf\ufdf0-\uffef])*([a-z]|\d|[\u00a0-\ud7ff\uf900-\ufdcf\ufdf0-\uffef])))\.)+(([a-z]|[\u00a0-\ud7ff\uf900-\ufdcf\ufdf0-\uffef])|(([a-z]|[\u00a0-\ud7ff\uf900-\ufdcf\ufdf0-\uffef])([a-z]|\d|-|\.|_|~|[\u00a0-\ud7ff\uf900-\ufdcf\ufdf0-\uffef])*([a-z]|[\u00a0-\ud7ff\uf900-\ufdcf\ufdf0-\uffef])))\.?$/i); return pattern.test(emailaddress); };
this function work in case
to call this
if( !isvalidemailaddress( emailaddress ) ) { /* stuff here */ }
keep in mind no 100% regex email check exists!
checkout link
Comments
Post a Comment