user330885
user330885

Reputation:

Regex - With Space and Special Characters

I'm using the following Regex ^[a-zA-Z0-9]\s{2,20}$ for input

The input length must be a least 2 characters and maximum 20 characters.

I also want to enable space in the input, but only space, not new line, etc.

Last thing I have problem with is that I want to enable characters such as !@#$%^&*)(

Upvotes: 4

Views: 38891

Answers (5)

Abhinav
Abhinav

Reputation: 1

*** Regex for all types of special characters and normal characters too with space in between them.

Declare the below in some variable and perform your task:

/^[a-zA-Z0-9 !@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]{2,20}$/

*** Regex for all types of special characters with spaces:

/^[ !@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]{2,20}$/

(answer is tried and tested!!)

Upvotes: 0

Osama Hashem
Osama Hashem

Reputation: 90

All of Special Characters and char and number and with Space

[A-Za-z0-9-.& ,+!@#$%\^*();\/|<>"'?=:\t_\n[]{}~`]

Upvotes: 0

Monday
Monday

Reputation: 1413

add characters to your regex code like this~

^[a-zA-Z0-9 !@#$%^&*)(]{2,20}$

the \s is not only express space..

Upvotes: 4

Hnatt
Hnatt

Reputation: 5935

Regarding the second part of your question, just put those characters inside of [], no escaping needed.

Upvotes: 0

Shi
Shi

Reputation: 4258

Try ^[a-zA-Z0-9 ]{2,20}$.

And are you sure your original expression worked? The quantifier {2,20} is only applied to the \s, and not to your set inside [].

Upvotes: 1

Related Questions