简体   繁体   English

Javascript 使用正则表达式的表单验证邮寄地址不起作用!

[英]Javascript form validation mailing address with regular expression not working!

Hello I have a question with javascript form validation and regular expressions.您好,我对 javascript 表单验证和正则表达式有疑问。 Below is the code I used.下面是我使用的代码。 I am trying to validate that an address has a number and a letter from the alphabet.我正在尝试验证地址是否有数字和字母表中的字母。 This code is for experimentation and does not need to do any ultra secure validation.此代码用于实验,不需要进行任何超安全验证。

Problem is the below code doesn't seem to work.问题是下面的代码似乎不起作用。 No matter what I do the alert box tells me I have to enter a valid address.无论我做什么,警报框都会告诉我我必须输入一个有效地址。 What is wrong.怎么了。 Isn't the regular expression searching the value and finding false?正则表达式不是搜索值发现false吗? Even if I enter a normal address like 123 Sky Rd.即使我输入像 123 Sky Rd 这样的普通地址。 it still outputs Please enter a valid address.它仍然输出请输入有效地址。

I hope my question is clear.我希望我的问题很清楚。 Below is the code to really clarify what is going on.下面是真正阐明发生了什么的代码。 Why is this not valid?为什么这无效?

 if (document.customerInfo.address.value == ""){
        msg += "Please enter a valid address\n";
        }
    else if (document.customerInfo.address.value.match(/[0-9]/) != true)
            {msg += "Please enter a valid address\n";}
    else (document.customerInfo.address.value.match(/[abc]/) != true)
            {msg += "Please enter a valid address\n";}

This code is wrong in a number of ways.这段代码在很多方面都是错误的。 First of all, match() doesn't return true or false.首先,match() 不返回 true 或 false。 It returns null or an array of matches.它返回 null 或匹配数组。 Second, your regular expression is looking for either a string of all numbers or a string of all characters and you have a string of both (including spaces).其次,您的正则表达式正在查找所有数字的字符串或所有字符的字符串,并且您拥有两者的字符串(包括空格)。

What are you really trying to test for?你真正想测试什么?

A single regex of /[a-zA-Z0-9 ]+/ will allow numbers, letters and spaces, but I don't know why you're even checking that. /[a-zA-Z0-9 ]+/ 的单个正则表达式将允许数字、字母和空格,但我不知道你为什么还要检查它。 An address isn't something that can really be checked this way.地址不是真正可以通过这种方式检查的东西。 You can make sure the field isn't empty, but addresses can take all sorts of forms including characters like # as in Suite #100.您可以确保该字段不为空,但地址可以采用各种 forms,包括像 Suite #100 中的 # 这样的字符。 I think all you can really do here is check that it's not empty.我认为您在这里真正可以做的就是检查它是否为空。

Try ([0-9]) and ([az])尝试([0-9])([az])

if (document.customerInfo.address.value == ""){
        msg += "Please enter a valid address\n";
        }
    else if (document.customerInfo.address.value.match(/([0-9])/) != true)
            {msg += "Please enter a valid address\n";}
    else (document.customerInfo.address.value.match(/([a-z])/) != true)
            {msg += "Please enter a valid address\n";}

Try this regex:试试这个正则表达式:

if (document.customerInfo.address.value == ""){
    msg += "Please enter a valid address\n";
}
else if (document.customerInfo.address.value.match/[a-zA-Z0-9 ]+/) == null) {
    msg += "Please enter a valid address\n";
}

You are matching only one character and only lower case [az] .您仅匹配一个字符且仅匹配小写[az] The '+' specifies one or more. '+' 指定一个或多个。

You have if, else if, else.你有如果,否则如果,否则。 You are appending the "Please enter a valid address" in all parts of the if statement, even in the else.您在 if 语句的所有部分中附加了“请输入有效地址”,即使在 else 中也是如此。

Change the code to be将代码更改为

if (document.customerInfo.address.value == ""){
    msg += "Please enter a valid address\n";
    }
else if (document.customerInfo.address.value.match(/[0-9]/) != true)
        {msg += "Please enter a valid address\n";}
else if (document.customerInfo.address.value.match(/[abc]/) != true)
        {msg += "Please enter a valid address\n";}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM