Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex pattern allowing alphabets, numbers, decimals, spaces and underscore

I am not good with regular expression patterns. I have to put a validation on an string, to allow

only alphabets, numbers, decimals, spaces, comma and underscore 

for allowing the alphabets and spaces I have /^[a-zA-Z][a-zA-Z\\s]+$/ Please help me in creating all the above conditions in one pattern.

Thanks

like image 407
Sashi Kant Avatar asked Dec 15 '22 15:12

Sashi Kant


1 Answers

this regex should work for your requirements

'[a-zA-Z0-9_. ,]*'

In the regex, I specified the range a to z, A to Z (uppercase), 0 to 9 and the single character _, decimal point ".", space and a comma.

If you want to make sure you want at least one character after the first letter, you can replace the * with a +, or {2,} with at least 2 more characters, or {2,5} with between 2 and 5 characters.

like image 51
Rahul Avatar answered Jan 21 '23 16:01

Rahul