Suhas Gosavi
Suhas Gosavi

Reputation: 2170

Regex for alphanumeric and some Special Characters

I need regex -

Requirement -

1) one alphabets compulsory

2) one number compulsory

and some special characters are also allowed its not compulsory.

special characters - [-!$^&*()_|~=`{}[]:/<>?,.@#]

I have tried

/((^\S*[0-9]+\S*[a-z]+\S*)|(^\S*[a-z]+\S*[0-9]+\S*))+$/i

but how to restrict that with given special characters only.

Upvotes: 2

Views: 361

Answers (1)

Toto
Toto

Reputation: 91385

Use lookahead:

/^(?=.*\d)(?=.*[a-zA-Z]+)(?=.*[-!$^&*()_|~=`{}\[\]:/<>?,.@#]+)[-\w!$^&*()|~=`{}\[\]:/<>?,.@#]+$/

If special char are not compulsory:

/^(?=.*\d)(?=.*[a-zA-Z]+)[-\w!$^&*()|~=`{}\[\]:/<>?,.@#]+$/

Upvotes: 1

Related Questions