javascript - Submit the form only if email is valid -
i need way submit form if email valid. if email not valid show alert ( below can see code ).
javascript:
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); } function continueornot() { if(validateemail(document.getelementbyid('emailfield').value)){ // ok }else{ alert("email not valid"); return false;} }
html:
<form method="post" action="register.php" enctype="multipart/form-data"> <table> <tr><td>email:</td></tr> <tr><td><input type="text" size="30" name="email" id="emailfield"> </td></tr> <tr><td>password:</td></tr> <tr><td><input type="password" size="30" name="password"> </td></tr> </table> <input name="steptwo" value="create account" type="submit" onclick="continueornot();"/> </form>
the problem form submitted if email not valid.
how can resolve problem ?
you should attach return continueornot();
onsubmit
event of form, not button's click event.
see fiddle: http://jsfiddle.net/ndsmu/
Comments
Post a Comment