Regex pattern to match at least 1 number and 1 character in a string

JavascriptRegexValidation

Javascript Problem Overview


I have a regex

> /^([a-zA-Z0-9]+)$/

this just allows only alphanumerics but also if I insert only number(s) or only character(s) then also it accepts it. I want it to work like the field should accept only alphanumeric values but the value must contain at least both 1 character and 1 number.

Javascript Solutions


Solution 1 - Javascript

Why not first apply the whole test, and then add individual tests for characters and numbers? Anyway, if you want to do it all in one regexp, use positive lookahead:

/^(?=.*[0-9])(?=.*[a-zA-Z])([a-zA-Z0-9]+)$/

Solution 2 - Javascript

This RE will do:

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

Explanation of RE:

  • Match either of the following:
  1. At least one number, then one letter or
  2. At least one letter, then one number plus
  • Any remaining numbers and letters

  • (?:...) creates an unreferenced group
  • /i is the ignore-case flag, so that a-z == a-zA-Z.

Solution 3 - Javascript

I can see that other responders have given you a complete solution. Problem with regexes is that they can be difficult to maintain/understand.

An easier solution would be to retain your existing regex, then create two new regexes to test for your "at least one alphabetic" and "at least one numeric".

So, test for this :-

/^([a-zA-Z0-9]+)$/

Then this :-

/\d/

Then this :-

/[A-Z]/i

If your string passes all three regexes, you have the answer you need.

Solution 4 - Javascript

While the accepted answer is correct, I find this regex a lot easier to read:

REGEX = "([A-Za-z]+[0-9]|[0-9]+[A-Za-z])[A-Za-z0-9]*"

Solution 5 - Javascript

The accepted answers is not worked as it is not allow to enter special characters.

Its worked perfect for me.

^(?=.*[0-9])(?=.*[a-zA-Z])(?=\S+$).{6,20}$

  • one digit must
  • one character must (lower or upper)
  • every other things optional

Thank you.

Solution 6 - Javascript

This solution accepts at least 1 number and at least 1 character:

[^\w\d]*(([0-9]+.*[A-Za-z]+.*)|[A-Za-z]+.*([0-9]+.*))

Solution 7 - Javascript

Maybe a bit late, but this is my RE:

/^(\w*(\d+[a-zA-Z]|[a-zA-Z]+\d)\w*)+$/

Explanation:

\w* -> 0 or more alphanumeric digits, at the beginning

\d+[a-zA-Z]|[a-zA-Z]+\d -> a digit + a letter OR a letter + a digit

\w* -> 0 or more alphanumeric digits, again

I hope it was understandable

Solution 8 - Javascript

And an idea with a negative check.

/^(?!\d*$|[a-z]*$)[a-z\d]+$/i
  • ^(?! at start look ahead if string does not
  • \d*$ contain only digits | or
  • [a-z]*$ contain only letters
  • [a-z\d]+$ matches one or more letters or digits until $ end.

Have a look at this regex101 demo

(the i flag turns on caseless matching: a-z matches a-zA-Z)

Solution 9 - Javascript

If you need the digit to be at the end of any word, this worked for me:

/\b([a-zA-Z]+[0-9]+)\b/g
  • \b word boundary
  • [a-zA-Z] any letter
  • [0-9] any number
  • "+" unlimited search (show all results)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionOM The EternityView Question on Stackoverflow
Solution 1 - JavascriptphihagView Answer on Stackoverflow
Solution 2 - JavascriptRob WView Answer on Stackoverflow
Solution 3 - JavascriptPaul Alan TaylorView Answer on Stackoverflow
Solution 4 - JavascriptNeuron - Freedom for UkraineView Answer on Stackoverflow
Solution 5 - JavascriptPratik ButaniView Answer on Stackoverflow
Solution 6 - Javascriptuser2043372View Answer on Stackoverflow
Solution 7 - JavascriptPablo GarciaView Answer on Stackoverflow
Solution 8 - Javascriptbobble bubbleView Answer on Stackoverflow
Solution 9 - JavascriptwillView Answer on Stackoverflow