Not Equal Sign Not Recognized When Validating Javascript Forms

Can someone please tell me how I can improve this code, and most importantly, sort my email check to work in all cases when Z is neither equal to anything nor the line "Email"

All fields begin with the appropriate wording already entered as an example for customers.

Regards.

function validateForm() { //Uses HTML field IDs var x=document.forms["myForm"]["name"].value; var y=document.forms["myForm"]["phone"].value; var z=document.forms["myForm"]["email"].value; //Name locator if (x==null || x=="" || x=="Name") { alert("Please enter the your name."); return false; } //Contact method locator if ((y==null || y=="" || y=="Phone Number")&&(z==null || z=="" || z=="Email")) { alert("Please enter a contact method."); return false; } //Phone numeric validation, this runs if Email field is not edited if (z==null || z=="" || z=="Email") { if (isNaN(y)||x.indexOf(" ")!=-1) { alert("Telephone must be a numeric value."); return false; } } //Phone length validation, this runs if Email field is not edited if (z==null || z=="" || z=="Email") { if (y.length > 14) { alert("Telephone must be valid."); return false; } } //Email validation, does not work, this should run only when something is entered into the field if (z!=null || z!="" || z!="Email") { var atpos=z.indexOf("@"); var dotpos=z.lastIndexOf("."); if (atpos<1 || dotpos<atpos+2 || dotpos+2>=z.length) { alert("This is not a valid e-mail address"); return false; } } } 
+4
source share
4 answers

You can do a few things.

z==null || z=="" z==null || z=="" can be replaced with !Boolean(z) or !z

z!=null || z!="" z!=null || z!="" can be replaced with Boolean(z) or !!z

You should also try to always use === instead of ==, unless your expectation is a type of coercion.

So, your check for z == "Email" may change to something like this z.toLowerCase() === "email"

It also seems like you are repeating the code -> z==null || z=="" || z=="Email" z==null || z=="" || z=="Email" z==null || z=="" || z=="Email" (x2). You can combine a phone number check and a phone length check.

+5
source

Check out this validation code , which is a combination of jQuery and Javascript. He will shed some insight into what you need to look for and how to handle certain elements of a typical form (including the GREAT RegEx example for an email address:

 /^((([az]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([az]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([az]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([az]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([az]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([az]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([az]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([az]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([az]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([az]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))$/i.test(value) 
+2
source

Edit:

 if (z!=null || z!="" || z!="Email") 

For this:

 if (z!=null && z!="" && z!="Email") 

Your check should be ANDs vs ORs, since you want all this to be true before submitting.

+1
source

Instead of manually writing form validation, have you considered using something like validate.js ?

Code example:

 var validator = new FormValidator('example_form', [{ name: 'req', display: 'required', rules: 'required' }, { name: 'alphanumeric', rules: 'alpha_numeric' }, { name: 'password', rules: 'required' }, { name: 'password_confirm', display: 'password confirmation', rules: 'required|matches[password]' }, { name: 'email', rules: 'valid_email' }, { name: 'minlength', display: 'min length', rules: 'min_length[8]' }], function(errors, event) { if (errors.length > 0) { // Show the errors } }); 
+1
source

Source: https://habr.com/ru/post/1498404/


All Articles