Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular Expression For At Least One Number

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.

like image 726
Prashobh Avatar asked Jun 27 '13 12:06

Prashobh


People also ask

How do you check if a string has at least one number?

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.

What does regex 0 * 1 * 0 * 1 * Mean?

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.

What does ?= * Mean in regex?

?= 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).


1 Answers

Try using this pattern

.*[0-9].* 

For 6 to 20 use this

^(?=.*\d).{6,20}$  
like image 185
code_rum Avatar answered Sep 21 '22 14:09

code_rum