Regular Expression: password must contain at least 1 special character - javascript

I am trying to verify that the password inserted by the user has atleast 1 special character in it. Can anyone help me with the regExp?
var password = document.getElementById(.....).value;
var special = new RegExp("?????");
if(special.test(password)){
......
}

I consider all the non-word characters as special characters.
var special = new RegExp("^.*?\\W");
OR
var special = /^.*?\W/;

use this way.
var passwd = document.getElementById('password').value;
var pattern = new RegExp(/[~`!#$%\^&*+=\-\[\]\\';,/{}|\\":<>\?]/);
if (pattern.test(passwd)) {
// code goes here
}

Related

Javascript regular expression only accept specific characters

I need to validate a field when a form is submitted, but only specific characters are allowed. Those characters are:
a-z A-Z 0-9 % . " ' & - # # $ * / + = [ ] !
I thought the following regex would work:
var regex = new RegExp(/[a-zA-Z0-9%\. "'&##\$\*\/\+=\[\]\!-]+/);
I tested it out https://regexr.com/4kigt, and it seems to match the pattern. However, when I attempt to validate on form submit, it doesn't fail when characters like (, ), or ? are entered. For example, when I enter (afasdf) into the form field, the match is true.
Any idea how to to fix this?
<input type="text" class="field">
<p>
<button>Validate</button>
</p>
var field = document.querySelector('.field');
var button = document.querySelector('button');
var regex = new RegExp(/[a-zA-Z0-9%\. "'&##\$\*\/\+=\[\]\!-]+/, 'g');
button.addEventListener('click', function() {
console.log(regex.test(field.value));
}, false);
Jsbin link https://jsbin.com/forupos/edit?html,js,console,output.
It returns true for (afasdf), but it actually matches afasdf. You should use ^n and n$ anchors.
var regex = new RegExp(/^[a-zA-Z0-9%\. "'&##\$\*\/\+=\[\]\!-]+$/);

How to remove character after # in javascripts?

How to remove character after # in javascripts ?
for example, I have string 'babibu#blabla.com', I want to get string before # 'babibu', how to do that?
There are various ways to do this:
1. Regex
Use the regex /^(.*)#/
Regex explanation
Demo:
var email = 'babibu#blabla.com';
var username = email.match(/^(.*)#/)[1];
console.log(username);
2. Array.split()
var email = 'babibu#blabla.com';
var username = email.split('#', 1)[0];
console.log(username);
Alternative to regex:
var username = email.split('#')[0];

passsword validation and alert message in Installshield javascript

Can anyone tell me how to print a message in installshield supported javascript instead of alert?
I tried with the below code but alert is'nt working.
And another problem is that I am unable to validate password by using RegExp.
function Passwordvalidation()
{
var password= Session.Property("PASSWORD");
var patt = new RegExp(":\\[A-Za-z0-9]{6,20}$\\","ig");
var validpassword = patt.test(password);
if(validpassword)
{
GetMD5(); //calls another function
return true;
}
alert("password should have 6 to 20 characters which contains alphabets and digits between 0 to 9");
return false;
}
As #some suggested, you should first only check if the regex works.
But reading the error message "password should have 6 to 20 characters which contains alphabets and digits between 0 to 9" , I think that this part of the regex [A-Za-z0-9]{6,20}$ does not do what you think it does.
This matches any uppercase or lowercase character or digit repeated between 6 and 20 times at the end of the string.
This would also match $$$$$444444 or aaaaaa
For example:
var patt = new RegExp('[a-z0-9]{6,20}$', "i");
var passwords = [
"aaaaaa",
"333333",
"a12b3c",
"AAAAAA",
"$$$$$444444"
];
for (var i = 0; i < passwords.length; i++) {
console.log(passwords[i] + " : " + patt.test(passwords[i]));
}
With ^:
var patt = new RegExp('^[a-z0-9]{6,20}$', "i");
var passwords = [
"aaaaaa",
"333333",
"a12b3c",
"AAAAAA",
"$$$$$444444"
];
for (var i = 0; i < passwords.length; i++) {
console.log(passwords[i] + " : " + patt.test(passwords[i]));
}
The modifier i makes it case insensitive, so you could update your regex to [a-z0-9]{6,20}$ or [A-Z0-9]{6,20}$
You can also omit the g modifier to prevent wrong results.
It is probably because var patt = new RegExp(":\\[A-Za-z0-9]{6,20}$\\","ig"); throws an exception since that is an illegal regexp.
You don't need to use new RegExp but can define it as a literal regexp:
var patt = /^[A-Za-z0-9]{6,20}$/;
Since you defined ranges A-Z and a-z you don't need the i flag, and you don't need the g flag either.
This answer explains password restriction validation with regex really nicely:
Let's say that we want our password to:
Contain between 8 and 15 characters
Must contain an uppercase letter
Must contain a lowercase letter
Must contain a digit
Must contain one of special symbols
Then we can write a regex like this:
^(?=.{8,15}$)(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])(?=.*[!##$%^&*]).*$
\__________/\_________/\_________/\_________/\______________/
length upper lower digit symbol

why is my regular expression not validating a valid expression?

I am at a lost as to why this will not.
here is my regular expression:
^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[\\\+\=\.\[\]_£|`¬':;~{}<>()#?!#$%^&*-]).{8,20}$
here is some code to simply test it:
var str1 = "AAbb123.";
var str2 = "ell";
var re = new RegExp("^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[\\\+\=\.\[\]_£|\`¬':\;\~{}<>()#?!\#$\%^&*-]).{8,20}$");
if(str1.match(re)){
alert("matched")
}
else {
alert("doesnt match")
}
the regular expression has been validated in 2 regular expression web sites (regexpal.com & http://www.freeformatter.com/regex-tester.html). both say str1 is valid for this expression but yet when included in my code it will not work.
below is another place I am trying to get the code working. and it keeps printing: requirements not met.
var uname = document.getElementById("pword1").value;
var re = new RegExp ("^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[\\\+\=\.\[\]_£|\`¬':\;\~{}<>()#?!\#$\%^&*-]).{8,20}$");
if(uname.match(re)){
DIMR = "Requirements MET";
}else {
DIMR = "Requirements NOT MET";
}
You need to properly escape a string when using new RegExp constructor.
Since you don't have any variables inside your pattern try
var str1 = "AAbb123.";
var str2 = "ell";
var re = /^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[\\\+\=\.\[\]_£|\`¬':\;\~{}<>()#?!\#$\%^&*-]).{8,20}$/;
if(str1.match(re)){
alert("matched")
}
else {
alert("doesnt match")
}
Escaping only few characters present inside the character class would be enough. When using " as regex delimiter, you need to escape the backslash in your regex one more time.
var re = new RegExp("^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[\\\\+=.\\[\\]_£|`¬':;~{}<>()#?!#$%^&*-]).{8,20}$");
special characters like +, ., * inside a character class would must match a literal + or . or *, so you don't need to escape it. To match a literal \, you need to escape that \ exactly three times.

Java Script - Regex - Password validation with Hebrew characters

I've got an issue with the password validation.
That's my code:
function validatePassword(){
var password = document.getElementById("password").value;
var re = /^(?=.*[0-9])(?=.*[!##$%^&*])[a-zA-Z0-9!##$%^&*]{6,16}$/;
if(!password.match(re)){
producePromt("The password is invalid","commandPasswordPrompt","red");
return false;
}
producePromt("Password is OK","commandPasswordPrompt","green");
return true;
}
It says that its only invalid, So I thought that its because of the regex.
I asked you if you can help with everything here.
Thanks a lot for helpers!
Try this
// At least eight numbers or/and letters of English or Hebrew language
^[a-zA-Z0-9\u0590-\u05FF]{8,}$
Or
// At least eight characters: one number, one uppercase, one lowercase English letter and one Hebrew letter
^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[\u0590-\u05FF]).{8,}$
Or
// At least eight characters: one number and one uppercase of lowercase English or Hebrew letter
^(?=.*[0-9])(?=.*[a-z|A-Z|\u0590-\u05FF]).{8,}$
Usage:
var p = /^(?=.*[0-9])(?=.*[a-z|A-Z|\u0590-\u05FF]).{8,}$/g;
var s = "שלוםWorld2";
if(!p.test(s)){
console.log("Invalid password!");
}
Regex Demo | jsBIn Demo
Maybe you could check it twice to make it easy.
For example:
var password = document.getElementById("password").value;
var re1 = /^a-zA-Z0-9!##$%^&*]{6,16}$/;
var re2 = /[0-9]/
var re3 = /[!##$%^&*]/
if(password.match(re1) && password.search(re2) >=0 && password.search(re3) >=0){
producePromt("The password is invalid","commandPasswordPrompt","red");
return false;
}
producePromt("Password is OK","commandPasswordPrompt","green");

Categories