最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Require both letters and numbers - regExp - Stack Overflow

programmeradmin1浏览0评论

I am trying to figure how to require both letters and numbers only without any other characters. So literally [a-z] and ( \d or [0-9] ) depending what is better way of doing it for numbers.

So if I had a string that requires validation:

$toValidate = 'Q23AS9D0APQQ2'; // It may start with letter or number, both cases possible.

And then if I had validation for it:

return /([a-z].*[0-9])|([0-9].*[a-z])/i.test($toValidate);

I used an i flag here because it could be that user enters it lowercase or uppercase, it's user preference... So that regex fails... It accepts special characters also, so that is not desired effect.

With the validation above, this passes as well:

$toValidate = 'asdas12312...1231@asda___213-1';

Then I tried something crazy and I don't even know what I have done, so if anyone could tell me beside the correct answer, I'll truly appreciate.

return /([a-z].*\d)+$|(\d.*[a-z])+$/i.test($toValidate);

This seemed to work great. But then when I tried to continue typing letters or numbers after an special character it still validates as true.

Example:

$toValidate = 'q2IasK231@!@!#_+123';

So please help me understand regularExpressions better and tell me what is the way to validate the string at the beginning of my question. Letters and numbers expected in the string.

I am trying to figure how to require both letters and numbers only without any other characters. So literally [a-z] and ( \d or [0-9] ) depending what is better way of doing it for numbers.

So if I had a string that requires validation:

$toValidate = 'Q23AS9D0APQQ2'; // It may start with letter or number, both cases possible.

And then if I had validation for it:

return /([a-z].*[0-9])|([0-9].*[a-z])/i.test($toValidate);

I used an i flag here because it could be that user enters it lowercase or uppercase, it's user preference... So that regex fails... It accepts special characters also, so that is not desired effect.

With the validation above, this passes as well:

$toValidate = 'asdas12312...1231@asda___213-1';

Then I tried something crazy and I don't even know what I have done, so if anyone could tell me beside the correct answer, I'll truly appreciate.

return /([a-z].*\d)+$|(\d.*[a-z])+$/i.test($toValidate);

This seemed to work great. But then when I tried to continue typing letters or numbers after an special character it still validates as true.

Example:

$toValidate = 'q2IasK231@!@!#_+123';

So please help me understand regularExpressions better and tell me what is the way to validate the string at the beginning of my question. Letters and numbers expected in the string.

Share Improve this question asked Aug 1, 2015 at 11:56 dvldendvlden 2,4628 gold badges39 silver badges61 bronze badges 3
  • 1 If you don't want to allow arbitrary characters then stop using . which is explicitly any character. – Dave Newton Commented Aug 1, 2015 at 12:01
  • @DaveNewton - I see, now that is something that wasn't explained in the original regEx that I found somewhere on StacksOverflow. Thank you. – dvlden Commented Aug 1, 2015 at 12:03
  • possible duplicate of Regex for alpanumeric with at least 1 number and 1 character – Anonymous Commented Aug 2, 2015 at 12:44
Add a comment  | 

3 Answers 3

Reset to default 14

To allow only letters and digits with at least one letter and at least one digit use:

/^(?=.*?\d)(?=.*?[a-zA-Z])[a-zA-Z\d]+$/

Regex breakdown:

^               # start of input
(?=.*?\d)       # lookahead to make sure at least one digit is there
(?=.*?[a-zA-Z]) # lookahead to make sure at least one letter is there
[a-zA-Z\d]+     # regex to match 1 or more of digit or letters
$               # end of input

RegEx Demo

You should not use .* in your regex otherwise it will allow any character in the input.

what you are looking for in pseudo is: START-ANCOR [a-zA-Z0-9] 0->Inf times , ([a-zA-Z][0-9] OR [0-9][a-zA-Z]), [a-zA-Z0-9] 0->Inf times END-ANCOR

in words, start with anything from your lang, end with anything from your lang and contain a seam between letters and digits or the other way around

Should be like this:

/^([a-z0-9]* (([a-z][0-9]) | ([0-9][a-z])) [a-z0-9]*)$/i.

You can combine more than one type of character within the brackets. So, the following regex should work:

/^([a-z0-9]+)$/i.

The ^ and $ match the start and end of the string, so the whole string will be tested for the conditions. The [a-z0-9] makes it match only letters and numbers. The + makes it match one or more character. And the "i" at the end, as you know, makes it case insensitive.

发布评论

评论列表(0)

  1. 暂无评论