Suffii
Suffii

Reputation: 5784

Having Issue on JS Regular Expression

I have a custom pattern like this

/^([a-zA-Z0-9@$().'"%#!&{}=+-_\n ]){3,300}$/

to validate user inputs in message box. As you can see I would like to allow some characters entry like @$().'"%#!&{}=+-_\n which are fine, but not characters like ^<>~ .

But adding the space to expression at ] is also validating those characters!

As you know, I have to validate the space in a message box. So can you please let me know how to fix this?

Upvotes: 0

Views: 41

Answers (2)

Anand Dwivedi
Anand Dwivedi

Reputation: 1500

Custom pattern like /^([a-zA-Z0-9@$().'"%#!&{}=+\-_\n ]){3,300}$/

Upvotes: 0

Ashish
Ashish

Reputation: 670

The problem is not the space character. The problem lies with the "-" (hyphen) you have added between "+" and "_". Hyphens are supposed to denote a range hence they should be escaped when not being used in that context.

The correct regex for you would be:

/^([a-zA-Z0-9@$().'"%#!&{}=+\-_\n ]){3,300}$/

Upvotes: 4

Related Questions