简体   繁体   中英

Javascript form validation mailing address with regular expression not working!

Hello I have a question with javascript form validation and regular expressions. 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? Even if I enter a normal address like 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. It returns null or an array of matches. 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. 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. I think all you can really do here is check that it's not empty.

Try ([0-9]) and ([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] . 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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