javascript - Validating e-mail address -
i trying validate e-mail address using javascript. problem if statements aren't executing correctly. if delete 'else statement' code runs correnctly , page not load errors. if include 'else statement' else statement never executes , status bar says page loads errors. wondering if can find errors unable pick up?
<h4>example 4:</h4> <div style="border:3px dashed #aaa; width:200px;"> <div id="text">e-mail: <input id="email" onblur="verifyemail()"/></div> <div id="verification" > </div> </div> <script type="text/javascript"> function verifyemail(){ var email = document.getelementbyid("email").value; var atpos=0; var dotpos=0; atpos = email.indexof("@"); dotpos = email.lastindexof("."); if(atpos<=3 || dotpos<atpos+2 || dotpos+2>=email.length){ document.getelementbyid("verification").innerhtml = "invalid e-mail address"; }else{ document.getelementbyid("verification").innerhtml = "valid"; } } </script>
try this
function validateemail(email) { var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\ ".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-za -z\-0-9]+\.)+[a-za-z]{2,}))$/; return re.test(email); } demo : here demo
or more simpler way
function validateemail(email) { var re = /\s+@\s+\.\s+/; return re.test(email); } if using html5 try <input type="email"... please note. 1 works if input in form tag submit button , isn't handled js
<input type="email" placeholder="me@example.com">
Comments
Post a Comment