JavaScript表单验证中无法识别“不相等”符号

vyu0f0g1  于 2023-11-15  发布在  Java
关注(0)|答案(4)|浏览(92)

有没有人能告诉我如何改进这段代码,最重要的是对我的电子邮件验证进行排序,使其在Z不等于nothing或字符串“Email”的所有情况下都能工作。
所有字段都以已输入的适当措辞开头,作为客户的示例。

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

字符串

pn9klfpd

pn9klfpd1#

你可以做一些事情。
z==null || z==""可以替换为!Boolean(z)!z
z!=null || z!=""可以替换为Boolean(z)!!z
你也应该尝试总是使用=而不是==,除非你期望类型强制转换。
因此,您对z == "Email"的检查可能会更改为类似这样的z.toLowerCase() === "email"
看起来你也重复了代码--> z==null || z=="" || z=="Email"(x2)。你可以将合并电话数字验证和电话长度验证结合起来。

e4yzc0pl

e4yzc0pl2#

看看this validation code,它是jQuery和JavaScript的组合。它将为您提供一些关于应该寻找什么以及如何处理典型表单的特定元素的优秀见解(包括电子邮件地址的GREAT RegEx示例:

/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\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)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))$/i.test(value)

字符串

kpbwa7wx

kpbwa7wx3#

变更:

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

字符串
为此:

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


您的验证应该都是AND与OR,因为您希望在提交之前所有这些都为真。

wztqucjr

wztqucjr4#

除了手工编写表单验证,你有没有考虑过使用像validate.js这样的东西?
示例代码:

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

字符串

相关问题