php - having issues to validate form using JavaScript -
on form when enter email address correctly , leaves name field blank, page submitting , javacript not running. otherwise working fine.
please me solve javascript.
function validateform(data) { var validfield = ""; var namevalid=/^[a-za-z ]+$/; if(data.name.value == ""){validfield += "- please enter name\n";} else if(data.name.value.search(namevalid)==-1){validfield += "- entered name contains numbers or symbols\n";} if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(data.email.value)){ return true; } validfield += "- please enter valid e-mail address\n"; alert(validfield); return false; } <form name="data" onsubmit="return validateform(this);" action="some.php" method="post"> <div> <label>name:</label><input type="text" id="name" name="name" size="20" maxlength="20"/></div> <div> <label>e- mail address:</label><input type="text" id="email" name="email" size="20" maxlength="30"/></div> <input type="submit" name="submit" value="start now!"/> </form>
you need refactor code little bit. @ minute if email address valid return true out of function regardless of name field.
function validateform(data) { var validfield = ""; var isvalid = false; var namevalid = /^[a-za-z ]+$/; if (data.name.value == "") { validfield += "- please enter name\n"; } else if (data.name.value.search(namevalid) == -1) { validfield += "- entered name contains numbers or symbols\n"; } if (!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(data.email.value))) { validfield += "- please enter valid e-mail address\n"; }; if (validfield == "") { isvalid = true; } if (!isvalid) { alert(validfield); } return isvalid; }
Comments
Post a Comment