How would you create a regular expression for a value that should contain at least one number? The user can enter any special character, letter etc., but should contain at least one number.
I tried with pattern="[\w+]{6,20}"
and
(?=.*\d)(*[a-z])(*[A-Z]).{6,20}
Neither are working.
Use the RegExp. test() method to check if a string contains at least one number, e.g. /\d/. test(str) . The test method will return true if the string contains at least one number, otherwise false will be returned.
Basically (0+1)* mathes any sequence of ones and zeroes. So, in your example (0+1)*1(0+1)* should match any sequence that has 1. It would not match 000 , but it would match 010 , 1 , 111 etc. (0+1) means 0 OR 1.
?= is a positive lookahead, a type of zero-width assertion. What it's saying is that the captured match must be followed by whatever is within the parentheses but that part isn't captured. Your example means the match needs to be followed by zero or more characters and then a digit (but again that part isn't captured).
Try using this pattern
.*[0-9].*
For 6 to 20 use this
^(?=.*\d).{6,20}$
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With