1

I'm sure it's simple and I just don't see it. I've been searching for examples and as short and simple as they are, I can't seem to find my issue.

I wish to validate a Postal Code field and use the Canadian Postal code format. I found an expression I wish to use and it looks like the following:

var validZIP={
   "US":"^\d{5}([\-]?\d{4})?$",
   "UK":"^(GIR|[A-Z]\d[A-Z\d]??|[A-Z]{2}\d[A-Z\d]??)[ ]??(\d[A-Z]{2})$",
   "CA":"^([ABCEGHJKLMNPRSTVXY]\d[ABCEGHJKLMNPRSTVWXYZ])\ {0,1}(\d[ABCEGHJKLMNPRSTVWXYZ]\d)$"
}

Please note the CA which stands for CAnada in this case.

My onChange function calls the following method (onchange class checkValidPostal(this) from the input):

function checkValidPostal(input)
{
    var re =  new RegExp(validZIP["CA"]);
    var value = input.value.toUpperCase();  
    if (value.match(re))
    {
        input.value = value;
        return true;    
    }
    input.value = "";
    return false;
}

I have checked the RegEx line using:

http://www.regular-expressions.info/javascriptexample.html and it works great on that page, but for some reason I can't get it to work on mine!

Please help.

4
  • what are you passing in as input? Commented Mar 20, 2013 at 18:12
  • Why are you using match and not test ? Commented Mar 20, 2013 at 18:12
  • I was passing in values such as : m5m5m5 or M2M2M2 ... etc. I used some console.log statements to validate that the test is happening, and the values are getting through, but the test is returning false. Commented Mar 20, 2013 at 18:17
  • I tried using TEST, but for some reason I couldn't do re.test(value) because it would give me an error that the object ^([ABCEGH... doesn't have a function test Commented Mar 20, 2013 at 18:18

3 Answers 3

4

There's a problem : as you use strings instead of regex literals, you lack some escapements.

Besides, you probably want to use test instead of match.

You could fix that like this :

var validZIP={
   "US": /^\d{5}([\-]?\d{4})?$/,
   "UK": /^(GIR|[A-Z]\d[A-Z\d]??|[A-Z]{2}\d[A-Z\d]??)[ ]??(\d[A-Z]{2})$/,
   "CA": /^([ABCEGHJKLMNPRSTVXY]\d[ABCEGHJKLMNPRSTVWXYZ])\ {0,1}(\d[ABCEGHJKLMNPRSTVWXYZ]\d)$/
}

function checkValidPostal(input) {
    var re =  validZIP["CA"];
    var value = input.value.toUpperCase();  
    if (re.test(value)) {
        input.value = value;
        return true;    
    }
    input.value = "";
    return false;
}
Sign up to request clarification or add additional context in comments.

1 Comment

I was going to mention the use of RegExp#test over String#match as well. You can see this working at codepen.io/bbuck/pen/cBkou
2

Inside a string you'll need to double-escape your backslashes, else they are already escaped by the string and there are no backslashes remaining by the time the RegEx constructor gets the string.

2 Comments

are you saying I should change : "CA":"^([ABCEGHJKLMNPRSTVXY]\d[ABCEGHJKLMNPRSTVWXYZ])\ {0,1}(\d[ABCEGHJKLMNPRSTVWXYZ]\d)$" to this: `"CA":"^([ABCEGHJKLMNPRSTVXY]\\d[ABCEGHJKLMNPRSTVWXYZ])\\ {0,1}(\\d[ABCEGHJKLMNPRSTVWXYZ]\\d)$"' ?
You're right! That was the problem all along. I will be removing the quotes from the regex strings in the array. Thanks so much Bill!
0

Try putting pattern instead of strings in validZIP:

var validZIP={
   "US":/^\d{5}([\-]?\d{4})?$/,
   "UK":/^(GIR|[A-Z]\d[A-Z\d]??|[A-Z]{2}\d[A-Z\d]??)[ ]??(\d[A-Z]{2})$/,
   "CA":/^([ABCEGHJKLMNPRSTVXY]\d[ABCEGHJKLMNPRSTVWXYZ])\ {0,1}(\d[ABCEGHJKLMNPRSTVWXYZ]\d)$/
}

Comments

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.