To check alphanumeric with special characters
var regex = /^[a-zA-Z0-9_$#.]{8,15}$/;
return regex.test(pass);
But, above regex returns true even I pass following combination
asghlkyudet
78346709tr
jkdg7683786
But, I want that, it must have alphanumeric and special character otherwise it must return false for any case. Ex:
fg56_fg$
Sghdfi#90
You can replace a-zA-Z0-9_ with \w, and using two anchored look-aheads - one for a special and one for a non-special, the briefest way to express it is:
/^(?=.*[_$#.])(?=.*[^_$#.])[\w$#.]{8,15}$/
Use look-ahead to check that the string has at least one alphanumeric character and at least one special character:
/^(?=.*[a-zA-Z0-9])(?=.*[_$#.])[a-zA-Z0-9_$#.]{8,15}$/
By the way, the set of special characters is too small. Even consider the set of ASCII characters, this is not even all the special characters.
The dollar sign is a reserved character for Regexes. You need to escape it.
var regex = /^[a-zA-Z0-9_/$#.]{8,15}$/;
Related
I have constructed the following Regex, which allows strings that only satisfy all three conditions:
Allows alphanumeric characters.
Allows special characters defined in the Regex.
String length must be min 8 and max 20 characters.
The Regex is:
"^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[$#$!%*?&])[A-Za-z\d$#$!%*?&]$"
I use the following Javascript code to verify input:
var regPassword = new RegExp("^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[$#$!%*?&])[A-Za-z\d$#$!%*?&]$");
regPassword.test(form.passwordField.value);
The test() method returns false for such inputs as abc123!ZXCBN. I have tried to locate the problem in the Regex without any success. What causes the Regex validation to fail?
I see two major problems. One is that inside a string "...", backslashes \ have a special meaning, independent of their special meaning inside a regex. In particular, \d ends up just becoming d — not what you want. The best fix for that is to use the /.../ notation instead of new RegExp("..."):
var regPassword = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[$#$!%*?&])[A-Za-z\d$#$!%*?&]$/;
The other problem is that your regex doesn't match your requirements.
Actually, the requirements that you've stated don't really make sense, but I'm guessing you want something like this:
Must contain at least one lowercase letter, at least one uppercase letter, at least one digit, and at least one of the special characters $#$!%*?&.
Can only contain lowercase letters, uppercase letters, digits, and the special characters $#$!%*?&.
Total length must be between 8 and 20 characters, inclusive.
If so, then you've managed #1 and #2, but forgot about #3. Right now your regex demands that the length be exactly 1. To fix this, you need to add {8,20} after the [A-Za-z\d$#$!%*?&] part:
var regPassword = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[$#$!%*?&])[A-Za-z\d$#$!%*?&]{8,20}$/;
I want to strip everything except alphanumeric and hyphens.
so far i've got this but its not working:
String = String.replace(/^[a-zA-Z0-9-_]+$/ig,'');
any help appreciated?
If you want to remove everything except alphanum, hypen and underscore, then negate the character class, like this
String = String.replace(/[^a-zA-Z0-9-_]+/ig,'');
Also, ^ and $ anchors should not be there.
Apart from that, you have already covered both uppercase and lowercase characters in the character class itself, so i flag is not needed. So, RegEx becomes
String = String.replace(/[^a-zA-Z0-9-_]+/g,'');
There is a special character class, which matches a-zA-Z0-9_, \w. You can make use of it like this
String = String.replace(/[^\w-]+/g,'');
Since \w doesn't cover -, we included that separately.
Quoting from MDN RegExp documentation,
\w
Matches any alphanumeric character from the basic Latin alphabet, including the underscore. Equivalent to [A-Za-z0-9_].
For example, /\w/ matches 'a' in "apple," '5' in "$5.28," and '3' in "3D."
Im working on a password validation that should only allow a-z 0-9 and these characters "!"#$%&'()*+,-./:;<=>?#[\]^_{|}~`
I tried using a regex but I'm not too good with them and I wasnt sure if this is even possible or if Im not escaping the correct characters.
var allowedCharacters = /^[A-Za-Z0-9!"#$%&'()*+,-.\/:;<=>?#[\\]^_`{|}~]+$/;
if (!s.value.match(allowedCharacters)){
displayIllegalTextError();
return false;
}
You need to place the dash at the start or end of the regex, or it will try to create a character range (,-.). Then, a-Z isn't a valid range, you probably meant a-z. Also, you need to escape the closing brackets:
/^[A-Za-z0-9!"#$%&'()*+,.\/:;<=>?#[\\\]^_`{|}~-]+$/
Looking over the ascii chart here I see your regex could be reduced to this character range:
/^[\x21-\x7e]+$/
If you just want to learn special behavior of character classes, you should read up
on it via regex basic tutorials.
Note that class behavior differs amongst the different flavors.
Simpler and more to the point using unicode: ^[\u0021-\u007E]+$.
/^[\u0021-\u007E]+$/.test('MyPassword!') // returns true
/^[\u0021-\u007E]+$/.test('MyPassword™') // returns false
Now if you would like to go a few steps further and actually create a more complex validation such as: minimum length 8 characters and at least one lowercase, one uppercase, one digit and one special character:
^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[^a-zA-Z0-9])[\u0021-\u007E]{8,}$
This question already has answers here:
Why is this regex allowing a caret?
(3 answers)
Closed 1 year ago.
I am using javascript regex to do some data validation and specify the characters that i want to accept (I want to accept any alphanumeric characters, spaces and the following !&,'\- and maybe a few more that I'll add later if needed). My code is:
var value = userInput;
var pattern = /[^A-z0-9 "!&,'\-]/;
if(patt.test(value) == true) then do something
It works fine and excludes the letters that I don't want the user to enter except the square bracket and the caret symbols. From all the javascript regex tutorials that i have read they are special characters - the brackets meaning any character between them and the caret in this instance meaning any character not in between the square brackets. I have searched here and on google for an explanation as to why these characters are also accepted but can't find an explanation.
So can anyone help, why does my input accept the square brackets and the caret?
The reason is that you are using A-z rather than A-Za-z. The ascii range between Z (0x5a) and a (0x61) includes the square brackets, the caret, backquote, and underscore.
Your regex is not in line with what you said:
I want to accept any alphanumeric characters, spaces and the following !&,'\- and maybe a few more that I'll add later if needed
If you want to accept only those characters, you need to remove the caret:
var pattern = /^[A-Za-z0-9 "!&,'\\-]+$/;
Notes:
A-z also includesthe characters: [\]^_`.
Use A-Za-z or use the i modifier to match only alphabets:
var pattern = /^[a-z0-9 "!&,'\\-]+$/i;
\- is only the character -, because the backslash will act as special character for escaping. Use \\ to allow a backslash.
^ and $ are anchors, used to match the beginning and end of the string. This ensures that the whole string is matched against the regex.
+ is used after the character class to match more than one character.
If you mean that you want to match characters other than the ones you accept and are using this to prevent the user from entering 'forbidden' characters, then the first note above describes your issue. Use A-Za-z instead of A-z (the second note is also relevant).
I'm not sure what you want but I don't think your current regexp does what you think it does:
It tries to find one character is not A-z0-9 "!&,'\- (^ means not).
Also, I'm not even sure what A-z matches. It's either a-z or A-Z.
So your current regexp matches strings like "." and "Hi." but not "Hi"
Try this: var pattern = /[^\w"!&,'\\-]/;
Note: \w also includes _, so if you want to avoid that then try
var pattern = /[^a-z0-9"!&,'\\-]/i;
I think the issue with your regex is that A-z is being understood as all characters between 0x41 (65) and 0x7A (122), which included the characters []^_` that are between A-Z and a-z. (Z is 0x5A (90) and a is 0x61 (97), which means the preceding characters take up 0x5B thru 0x60).
I am working on this code and using "match" function to detect strength of password. how can I detect if string has special characters in it?
if(password.match(/[a-z]+/)) score++;
if(password.match(/[A-Z]+/)) score++;
if(password.match(/[0-9]+/)) score++;
If you mean !##$% and ë as special character you can use:
/[^a-zA-Z ]+/
The ^ means if it is not something like a-z or A-Z or a space.
And if you mean only things like !#$&$ use:
/\W+/
\w matches word characters, \W matching not word characters.
You'll have to whitelist them individually, like so:
if(password.match(/[`~!##\$%\^&\*\(\)\-=_+\\\[\]{}/\?,\.\<\> ...
and so on. Note that you'll have to escape regex control characters with a \.
While less elegant than /[^A-Za-z0-9]+/, this will avoid internationalization issues (e.g., will not automatically whitelist Far Eastern Language characters such as Chinese or Japanese).
you can always negate the character class:
if(password.match(/[^a-z\d]+/i)) {
// password contains characters that are *not*
// a-z, A-Z or 0-9
}
However, I'd suggest using a ready-made script. With the code above, you could just type a bunch of spaces, and get a better score.
Just do what you did above, but create a group for !##$%^&*() etc. Just be sure to escape characters that have meaning in regex, like ^ and ( etc....
EDIT -- I just found this which lists characters that have meaning in regex.
if(password.match(/[^\w\s]/)) score++;
This will match anything that is not alphanumeric or blank space. If whitespaces should match too, just use /[^\w]/.
As it look from your regex, you are calling everything except for alphanumeric a special character. If that is the case, simply do.
if(password.match(/[\W]/)) {
// Contains special character.
}
Anyhow how why don't you combine those three regex into one.
if(password.match(/[\w]+/gi)) {
// Do your stuff.
}
/[^a-zA-Z0-9 ]+/
This will accept only special characters and will not accept a to z & A to Z 0 to 9 digits