Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular Expression either numeric characters or both alpha-numeric but not only alpha characters

Tags:

regex

To validate a field that must have both numbers and letters, I could use:

/^([0-9]+[a-zA-Z]+|[a-zA-Z]+[0-9]+)[0-9a-zA-Z]*$/

But I want to additionally accept numeric only strings.

like image 715
max Avatar asked Dec 11 '25 14:12

max


2 Answers

You mean you want at least one number?

You could use look-ahead assertion.

/^(?=.*\d)[a-z\d]*$/i
like image 187
xdazz Avatar answered Dec 13 '25 22:12

xdazz


i got the answer from http://www.javascripter.net/faq/regularexpressionsyntax.htm

well i can do an "or" function inside regx

/^([0-9]|([0-9]+[a-zA-Z]+|[a-zA-Z]+[0-9]+)[0-9a-zA-Z]*)$/;
like image 35
max Avatar answered Dec 13 '25 20:12

max



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!