Unreliable javascript regex test in Firefox and Chrome
Maybe try putting your regex in a separate variable, like so:
//...
var re = /^\d{5}$/; // using Pointy's comment, which I think is well-advised
if (re.test(orgnValue)) { // This is the problem area
orgn.removeClass("invalid"); // The above line is '/\d{4}/g' for prog.
} else {
orgn.addClass("invalid");
}
//...
This is a known problem with some browsers when using a regexp object, cause by the lastIndex
property. You can easily reproduce it by:
var r = /\d{5}/g;
alert(r.test('12345')); //true
alert(r.test('12346')); //false
On you case, the regex is cached and you see the same effect. A simple solution is to reset the regexp lastIndex
: r.lastIndex = 0
, or, as suggested, to use a regex where this isn't an issue.