Regex javascript IE compatibility issue

This is about a strange behavior IE shows for a regex matching Im trying to do.

Here is a password matching expression Im trying to use for matching the below conditions.

  • Must contain minimum 8 to 20 characters, mandatorily including one letter and number
  • May include only one of the following special characters: %,&, _, ?, #, =, -
  • Cannot have any spaces

^(?=.{0,11}\d)(?=.{0,11}[a-zA-Z])[\w%&?#=-]{8,12}$

and the javascript to do the match is

function fnIsPassword(strInput){
alert("strInput : " + strInput);
var regExp =/^(?=.{0,11}\d)(?=.{0,11}[a-zA-Z])[\w%&?#=-]{8,12}$/;
if(strInput.length > 0){
return (regExp.test(strInput));
}
return false;
}

alert(fnIsPassword(“1231231”)); //false
alert(fnIsPassword(“sdfa4gggggg”)); //FF: true, false in IE
alert(fnIsPassword(“goog1234#”)); //FF: true , false in IE

The problem as stated above is that IE gives me false for the last 2 matches.

My IE version is 6.0.2900.5512.xpsp_sp3_gdr.090804-1435 on win XP ; ScriptEngineMajorVersion() = 5 ScriptEngineMinorVersion() = 7.
I got it reproduced on a colleague’s machine with the same configuration.

Somehow IE does the {8,12} count constraints after the first digit is matches.
Thus i see that abc45678901 is matched and abc4567890 is not matched ( notice that the counting starts from he first digit match)

Anyone can reproduce this issue ? Any workarounds ? .

Thanks in advance,
mays

Its a bug in IE.
Cant post a link here !!! Well,

This is what i found with some sifting.

  1. While finding the lower count for occurrences, counting starts from the end of the first lookahead match. Albeit this, final matches will be made only from the start of the string.
  2. For upper count, behavior is as expected.

My final solution is /(?=[1]{8,12}$)(?=.+\d)(?=.+[a-zA-Z]).+/


  1. \w%&?#=- ↩︎

Your expression needs to be a lot more complex to enforce the rules,
especially for including only one of the special characters.

You could simplify it by breaking it up into separate tests,
or you could use a match or exec on a single (very long) expression and
look at each segment.

function fnIsPassword(str){
if(/^([%&?#=\-a-zA-Z\d]+){8,20}$/.test(str)){
if(str.split(/[%&?#=\-]/).length> 2) return false;
return /[a-zA-Z]/.test(str) && /\d/.test(str);
}
return false;
}

I noticed the “_” in your problem description, but not in the final version. That normal?

@presotrader : Oops! missed ‘_’

@mrhoo : When i said ‘May include only one of the following special characters’, what was really meant was " can contain special chars only from this subset ". Apologizing for the ambiguity.

For academic sake, wondering what such a ‘lot more complex regex’ would look like.