Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex not allowing certain special characters

I have the following regex which does not allow certain special characters:

if (testString.match(/[`~,.<>;':"\/\[\]\|{}()-=_+]/)){    
    alert("password not valid");
}
else
{
    alert("password valid");
}

This is working. This regex will accept a password if it does not contain any of the special characters inside the bracket (~,.<>;':"\/\[\]\|{}()-=_+).

My problem here is it also don't allow me to input numbers which is weird.

Anything I missed here? Thanks in advance!

Here is a sample:

jsFiddle

like image 820
Gerald Avatar asked Aug 23 '13 08:08

Gerald


People also ask

How do I use regular expression to exclude special characters?

This article will illustrate how to use Regular Expression which allows Alphabets and Numbers (AlphaNumeric) characters with Space to exclude (not allow) all Special Characters. The following HTML Markup consists of an HTML TextBox and a Button. When the Button is clicked, the Validate JavaScript function is called.

How do I ignore a character in a string in regex?

This expression will ignore any string containing an a: /^(?!.*a).*/ If the character you want to exclude is a reserved character in regex (such as ? or *) you need to include a backslash \ in front of the character to escape it, as shown:

What is regex match all except a specific word?

Regex Match All Except a Specific Word, Character, or Pattern December 30, 2020 by Benjamin Regex is great for finding specific patterns, but can also be useful to match everything except an unwanted pattern. A regular expression that matches everything except a specific pattern or word makes use of a negative lookahead.

Is it possible to have a 1 character password in regex?

Regex can be tweaked to allow 1 character inout also but having length of 1 for password is not very common. @anubhava Thanks for saving my day ! Actually i am using this pattern for input validation where i needed to validate single char input also.


1 Answers

You've got a character range in there: )-= which includes all ASCII characters between ) and = (including numbers). Move the - to the end of the class or escape it:

/[`~,.<>;':"\/\[\]\|{}()=_+-]/

Also, you don't need to escape all of those characters:

/[`~,.<>;':"/[\]|{}()=_+-]/

Note that in your case, it is probably enough for you, to use test instead of match:

if (/[`~,.<>;':"/[\]|{}()=_+-]/.test(testString))){
    ...

test returns a boolean (which is all you need), while match returns an array with all capturing groups (which you are discarding anyway).

Note that, as Daren Thomas points out in a comment, you should rather decide which characters you want to allow. Because the current approach doesn't take care of all sorts of weird Unicode characters, while complaining about some fairly standard ones like _. To create a whitelist, you can simply invert both the character class and the condition:

if (!/[^a-zA-Z0-9]/.test(testString)) {
   ...

And include all the characters you do want to allow.

like image 186
Martin Ender Avatar answered Oct 25 '22 07:10

Martin Ender