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); 

demo


Comments

Popular posts from this blog

java - Run spring boot application error: Cannot instantiate interface org.springframework.context.ApplicationListener -

python - pip wont install .WHL files -

Excel VBA "Microsoft Windows Common Controls 6.0 (SP6)" Location Changes -