Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript RegExp compatibility in IE

Tags:

^(?=[\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 
like image 401
mays Avatar asked Dec 14 '09 14:12

mays


1 Answers

The regex has a number of issues. Try this one instead:

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

which is:

^                    # start-of-string
(?=.{,19}\d)         # look-ahead: a digit within 20 chars
(?=.{,19}[a-zA-Z])   # look-ahead: a letter within 20 chars
[\w%&?#=-]{8,20}     # 8 to 20 chars of your allowed range
$                    # end-of-string
like image 79
Tomalak Avatar answered Nov 10 '22 01:11

Tomalak