0

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

4 Answers 4

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I believe you are right and that it doesn't return a true. This would be the source of my problem. Thanks for the information!
Regex's are a good start, but if you could verify the validity of the address and bypass regex, would you rather do that? There are few free ways to do this, but SmartyStreets (the company I work with) has one called LiveAddress that would be a more reliable/practical solution than a regex, for the reasons that @jfriend00 stated.
0

Try ([0-9]) and ([a-z])

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

Comments

0

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

Comments

0

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 [a-z]. The '+' specifies one or more.

3 Comments

if (document.customerInfo.address.value == "") {msg += "Please enter a valid address\n";} else if (document.customerInfo.address.value.match(/[a-z]+[A-Z]+[0-9]+/) != true) {msg += "Please enter a valid address\n";}
I placed in the above and get the same problem
(/[a-z]+[A-Z]+[0-9]+/ isn't the same as /[a-zA-Z0-9 ]+/. Try my updated answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.