2

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.

0

1 Answer 1

1

Use lookahead:

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

If special char are not compulsory:

/^(?=.*\d)(?=.*[a-zA-Z]+)[-\w!$^&*()|~=`{}\[\]:/<>?,.@#]+$/ 
Sign up to request clarification or add additional context in comments.

1 Comment

Special characters are not compulsory.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.