javascript - Unable to stop form from submitting when validating an email -
i making simple form takes value text input, checks if characters in email address there , returns true or false. according lot of different resources, if returns false, form shouldn't submitted. however, when test in js bin, submits it. here's code:
function validateemail(x) { console.log(x); var email = x.mytext.value; console.log(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,}))$/; console.log(re.test(email)); } <form name="myform" onsubmit="return validateemail(this)"> <input name="mytext" type="text"> <input type="submit" value="submit"> </form>
you need return false if validation fails, replace
console.log(re.test(email)); with
return re.test(email);
Comments
Post a Comment