views:

418

answers:

2

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

Was meant to match the below conditions in a javascript based new-password check,

  • 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

With above regex,

goog123# was matched in FF3.5

However this fails in IE6. Anyone knows what went wrong here ? Is this a compatibility issue?

javascript used to test the matching

function fnIsPassword(strInput)
{
  alert("strInput : " + strInput);
  var regExp =/^(?=.{0,19}\d)(?=.{0,19}[a-zA-Z])[\w%&?#=-]{8,20}$/;
  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
+3  A: 
Tomalak
thanks tomalak for the quick help.however, the issue still remains.As i had specified earlier, im using regExp in javascript to do the matching.Pasting my test script herefunction fnIsPassword(strInput){ alert("strInput : " + strInput); var regExp =/^(?=.{0,19}\d)(?=.{0,19}[a-zA-Z])[\w% if(strInput.length > 0){ return (regExp.test(strInput)); } return false;}alert(fnIsPassword("1231231")); //false alert(fnIsPassword("sdfa4gggggg")); //FF: true, false in IE6 alert(fnIsPassword("goog1234#")); //FF: true , false in IE6and yes, IE6 is a pain.
mays
sorry for the formatting. as you might have guessed, im new around here.
mays
I tried your test script in JScript on Windows XP (IE6 uses JScript 5.6) and it correctly returns 0 (=false) for the first and -1 (=true) for the two other tests. Can you save it to a .js file and try on its own?
Tomalak
Can anyone else reproduce this issue ? It still throws false for all the tests for me. From my end , i got a colleague to reproduce this on his windows XP box. My IE version is 6.0.2900.5512.xpsp_sp3_gdr.090804-1435Ive also tried giving language="JScript" as you suggested.Feeling pwned :(
mays
And yes i had moved it in to a js file. Im confused because the same test succeeds in FF.
mays
What JScript version do you have? If less than 5.6, update to the current version and see if this helps.
Tomalak
ScriptEngineMajorVersion() = 5 ScriptEngineMinorVersion() = 7
mays
In IE , i see that the match is made if there is atleast 11 chars. I guess this has something to do with (?=) and {n} support in IE. ie : abc45678901 is matched and abc4567890 is not matched.
mays
mays
Somehow IE does the {8,12} count constraints after the first digit is matched. Thus the above solution will also not work. can anyone help me reproduce this issue ? Tomalak , which version of IE are you using?
mays
I'm on IE8, JScript 5.7. The IE version should not play a part in this, unless I'm much mistaken. All IE JavaScript handling is taken care of by the JScript engine installed on your system (correct me if I'm wrong). If it works in a .js file, it should work in IE.
Tomalak
It seems to be a JScript engine bug. But I'm not sure why the same version doesnt reproduce this on your machine. More about the bug here: http://regexadvice.com/blogs/mash/archive/2009/02/21/Looking-again-at-the-Lookahead-bug.aspx. Thanks a lot for your help tomalak.
mays
I'm sorry I couldn't help you better.
Tomalak
+1  A: 

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.

The final regex for this issue is

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

mays