1

I get an alert telling me the address needs to contain only alpha numeric chars..

What i have for the regex is /^\w+$/

so when i put 123 Lane Street

it gives me that error. Any ideas why it's doing this?

 if (address == ""){
                  errors += "please enter a address \n";
                } else { 
                    addressRE = /^\w+$/;
                    if (address.match(addressRE)){
                      //console.log("address match");
                      //do nothing.
                    } else {
                      //console.log("address not a match");
                      errors += "Address should only contain alphanumeric characters \n";
                    } // end if
                }
4
  • 1
    yes, it would show the error because the address contain spaces. Commented Jul 14, 2014 at 4:54
  • In addition, rubular.com is a quick and interactive way to learn regex. Try it out with the addresses above. you can use this expression also :^[a-zA-Z\d\s]* Commented Jul 14, 2014 at 5:33
  • @zx81 i can't bud. Don't have enough rep points :'( Commented Jul 16, 2014 at 23:33
  • ah, forgot all about that. Thank you kind sir Commented Jul 16, 2014 at 23:50

1 Answer 1

2

The space character in 123 Lane is not considered to be alphanumeric.

You need /^[a-z0-9 ]+$/i

The i turns on case-insensitive matching.

In JS:

if (/^[a-z0-9 ]+$/i.test(yourString)) {
    // It matches!
} else {
    // Nah, no match...
}
Sign up to request clarification or add additional context in comments.

2 Comments

I think your answer matches a single character.
@AvinashRaj You're totally right, I forgot the +. I often do that. Thanks a lot. :)

Your Answer

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