0

I would like to make regular expression so it matches only value as bellow, but I want to make it so you have to have at least one (i know it works with the "+" symbol) a-z letter, but I don't know where to put the "+" to make it work correctly. Any help on this?

.match(/^[0-9a-z]{3,10}$/i)

Edit: It should match for string like "12a", but it shouldn't match for "123" cause it has to have at least one a-z in it.

if( ! ($Username.match(/^[0-9a-z]{3,10}$/i) && $Username.match(/[a-z]/i)) ) {
    return false;
}

Edit: Thanks to Felix now the example code above works perfect.

5
  • 1
    It always helps with regex questions if you post a few strings that should match and a 1 or more should not match. Commented Jan 21, 2011 at 20:07
  • Good point, didn't think of it. Edited. Commented Jan 21, 2011 at 20:08
  • Some would deem “proper Javascript regex” to be oxymoronic. :) Commented Jan 21, 2011 at 20:20
  • @tchrist, what is "oxymoronic"? Commented Jan 21, 2011 at 20:25
  • 1
    It has to be $Username.match(/[a-z]/i). a-z must be in a character class, otherwise it matches a-z literally. Commented Jan 21, 2011 at 20:27

3 Answers 3

3

Assuming you mean:

  • Between 3 and 10 characters (inclusive)
  • All letters or numbers
  • At least one letter in any position

Then the simplest way to achieve this would be:

foo.match(/^[0-9a-z]{3,10}$/i) && foo.match(/[a-z]/i);
Sign up to request clarification or add additional context in comments.

5 Comments

@subtenante — thanks for the catch, fixed. This is what comes of too much exposure to jQuery ;)
Wondering if doing to reg exp matches is more efficient than doing one, but a little bit complicated?
@Richards — assuming that such a regex exists (nobody has suggested one that meets the outlined conditions so far), then simple beats efficient until there is a real performance problem. "premature optimization is the root of all evil" — Donald Knuth
There is none, don't look for it farther. What Richards describes is neither right-linear nor left-linear. Ergo : it's not regular.
@daviddorward, actually I'm having trouble to get your example working.. Added code to first post.
2

You can't do it in a single step. The expression you talk about is not regular. You have to make it in several steps. Just match with your current expression and combine to the match with a /[a-z]/ expression.

Comments

0

I believe this will do the filtering, although you'll have to check the character count separately:

^[0-9a-z]*[a-z]+[0-9a-z]*$

5 Comments

That regex doesn't even compile.
This does not enforce one letter.
Now that the OP has clarified the post, I've updated my answer to (i think) solve it
This does not enforce 3 to 10 characters ;)
Well to my credit -- While one might infer that from the OP's regex attempt, neither his copy nor his examples specified it :)

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.