Sashi Kant
Sashi Kant

Reputation: 13465

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

Upvotes: 5

Views: 7083

Answers (3)

Sashi Kant
Sashi Kant

Reputation: 13465

Rahul's answer gave me the direction to think, but for the visitors, may be this too can be helpfull

patternForClasName      =   /^([a-zA-Z0-9 _]+\.)*[a-zA-Z0-9 _]+$/;  
// Allowing valid className which has a format abcsasa.dsd.dsd(the class or a package name can have an underscore or a numerical)

patternForName          =   /^([a-zA-Z0-9 _-]+)$/;
// Allowing alphanumeric + spaces + (_)underscore and a (-)dash

patternForDescription   =   /^([a-zA-Z0-9 _-]+[\,\.]+)*[a-zA-Z0-9 _-]*$/;
  // Allowing alphanumeric,spaces, (_)underscore, (-)dash, comma, decimal

patternURLFormat        = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/;

// For a valid URL

Upvotes: 0

Rahul
Rahul

Reputation: 16335

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.

Upvotes: 8

PhiLho
PhiLho

Reputation: 41142

You can try:

/^[\w., ]+$/

I don't know what are the requirements for the starting char, if there are any.

Upvotes: 5

Related Questions