0

This is my code but for some reason it doesn't work anybody got a clue?

var val="My1name"
var exp = new RegExp("((?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{6,20})"); 
if(!val.match(exp)){      
   alert("No match")
} else {
   alert("Match")
}
3
  • 1
    Use the literal grammar for creating regular expressions if they're going to be constant. var exp = /((?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{6,20})/ Commented Feb 23, 2012 at 17:23
  • am not i am is right, you've got to escape some characters. But if you don't really need the new RegExp(...) way why not use /.../ instead? Commented Feb 23, 2012 at 17:24
  • What does "doesn't work" mean? "Doesn't work" is an inadequate description for us to understand the problem. What happened when you tried it? Did you get incorrect results? Did you get no results? If the results were incorrect, what made them incorrect? What were you expecting instead? Did you get any correct results? If so, what were they? Don't make us guess. Commented Aug 17, 2013 at 23:35

2 Answers 2

2

You need to escape the escape characters when building from a string...

var exp = new RegExp("((?=.*\\d)(?=.*[a-z])(?=.*[A-Z]).{6,20})"); 
 //-------------------------^
Sign up to request clarification or add additional context in comments.

Comments

0

Are you trying to do this?

var val = "My1name";
var exp = /((?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{6,20})/; 
if(!val.match(exp)){      
   alert("No match");
} else {
   alert("Match");
}

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.