The idea behind JavaScript form validation is to provide a method to check the user entered information before they can even submit it. JavaScript also lets you display helpful alerts to inform the user what information they have entered incorrectly and how they can fix it.
How to validate Empty Form Field
?
function IsEmpty(objectfield,stringfield) { objectvalue = objectfield.value.length; if(objectvalue=="") { alert("Oops.. Please fill out the value of "+stringfield); objectfield.style.background = 'Yellow'; return false; } else return true; }
How to Validate Email Address via JavaScript?
Other must read: https://crunchify.com/javascript-function-to-validate-username-phone-fields-on-form-submit-event/
function validate_email(field,alerttxt) { with (field) { apos=value.indexOf("@"); dotpos=value.lastIndexOf("."); if (apos<1||dotpos-apos<2){ alert(alerttxt);return false; } else { return true; } } }
How to Validate Password Field via JavaScript?
function validatePassword(fld) { var error = ""; var illegalChars = /[\W_]/; // allow only letters and numbers if (fld.value == "") { fld.style.background = 'Yellow'; error = "You didn't enter a password.\n"; alert(error); return false; } else if ((fld.value.length < 7) || (fld.value.length > 15)) { error = "The password is the wrong length. \n"; fld.style.background = 'Yellow'; alert(error); return false; } else if (illegalChars.test(fld.value)) { error = "The password contains illegal characters.\n"; fld.style.background = 'Yellow'; alert(error); return false; } else if ( (fld.value.search(/[a-zA-Z]+/)==-1) || (fld.value.search(/[0-9]+/)==-1) ) { error = "The password must contain at least one numeral.\n"; fld.style.background = 'Yellow'; alert(error); return false; } else { fld.style.background = 'White'; } return true; }
NOTE:
Please update your Form Field Name accordingly.