ı am looking for a regex code which can control at least 1 number when entering password.
ı am using this
var rakamKontrol = new RegExp(/^(?=.*[0-9])$/);
if (!rakamKontrol.test(r.newPassword)) {
alert("at least 1 number ..");
//but even ı enter number for password ı got error alert.
}
ı also try to
at least one special character,
at least one upper case ,
at least one lower case ,
at least 8 characters.
and ı want to show error messsages unique.
for lower case,for upper case etc.
The following set of tests include everything you mentioned. Using HereticMonkeys syntax just because it looks good.
regex 101 https://www.w3schools.com/jsref/jsref_obj_regexp.asp
if(!/\d/.test(r.newPassword)){
console.log('A password must contain at least one number');
}
if(!/[a-z]/.test(r.newPassword)){
console.log('A password must contain at least lower case letter');
}
if(!/[A-Z]/.test(r.newPassword)){
console.log('A password must contain at least upper case letter');
}
if(!/[!#=#$%&*)(_-]/.test(r.newPassword)){
console.log('A password must contain at least one special character');
}
if(r.newPassword.length < 8){
console.log('A password must be at least 8 characters long');
}
You can use password-validator library for defining your password rules. Eg:
var passwordValidator = require('password-validator');
var schema = new passwordValidator();
schema
.has().digits()
.has().uppercase()
const failedRules = schema.validate(r.newPassword, {list: true});
if (failedRules.includes('digits') {
alert('at least 1 number ..')
}
Disclaimer: I'm the maintainer of password-validator.
Related
I have a regex pattern that I'd like to apply to passwords:
/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[[:punct:]])./
There are 4 capture groups. I'd like to be able to know which of the capture groups don't match the supplied string, so I can give specific feedback to a user.
For example:
abcd43 -> 1st & 3rd groups true; 2nd and 4th false
(So I can customize an error: "Your password must contain at least one capital letter and a punctuation character.")
The simplest way is to check the four groups separately.
You can use something as easy as:
if(!lowerCase(pass))
print "No lower case letters"
else if(!upperCase(pass))
print "No upper"
else if(!digits(pass))
print "No digits"
etc...
Just set a varibale as your match group and check for every group:
var pattern =/...../ //whatever
var ma = 'my string'.match(pattern);
if (!ma)
console.log('No match');
else{
console.log(ma[0]) // first group.
console.log(m[1]); // second group;
}
Now simply check that each group has a value or not and each index represents the corresponding parenthesis in-order:
if (!m[0]){
alert('your password does not match first parenthesis');
}
Javascript does not directly support named capture groups in regular expressions. Your best bet then would be to simply check the groups, and map the numbers to a condition, perhaps through an array. So you might have 1 -> "lower case", 2 -> "upper case", etc.
Then just build a message corresponding to the failure(s), and display that message.
"lower case" -> "Passwords must contain at least one lower case letter"
"upper case" -> "Passwords must contain at least one upper case letter."
Now, with that said, PLEASE, PLEASE don't do this. I use a password generator, plus a password store, for my logins. If you're going to impose more restrictions than just length, please publish them all, right up front. Don't try to "customize" the error message to tell me what I'm missing. (And, realistically, just require a long password. If you require 16 characters with no other limits, you're more secure than 8 characters with 1 digit, 1 cap, and 1 punct.)
I am trying to write a regular expression to validate a password which must meet the following criteria:
a. Password must be 6 to 8 characters long, contain at least 3 alpha and 2 numeric characters and no special characters.
b. Must not contain the sequence ‘pas’.
What I've tried so far:
/^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])([a-zA-Z0-9]{8})$/
I suggest you to not use only one regex, because that way the users would not know why their password are failing.
I would do something like this:
function checkPassword (pass) {
pass = {
text: pass,
length: pass.length,
letters: pass.match(/[a-z]/gi).length,
digits: pass.match(/[0-9]/g).length,
contains_pas: !!pass.match(/pas/i) // <- will result true/false
}
pass.specials = pass.length - pass.letters - pass.digits;
// here you can use your logic
// example:
if (pass.contains_pas) {
alert('The password can not contains "pas".');
}
return pass; // you can return or not
}
Hope it helps.
You can try this:
([[a-zA-Z]{3,}+[0-9]{2}])^((?!pas).)$
It works only if user enters consecutive alphabets and then numbers. So, its a partial solution to this problem.
For the stated problem, I would suggest not to use reg-ex. As, reg-ex validates a particular order, you should incorporate separate checks for each test.
I need a regular expression to validate a password containing at least 8 characters, must include at least one uppercase letter and a lowercase letter. And must specifically include one of the following symbols #,#,%,^,&,*,)
i havent been able to find one that would include only those ascii characters.
thanks in advance for your help!
/^(?=.*[a-z])(?=.*[A-Z])(?=.*[!##$%^&*]).{8,}$/
Regular expression to assert a password that must contain atleast one Smallcase ,Capitalcase alphabet and a Special character(!##$%^&*).
Can increase the max length of password from 20 to more.
You can also put all your validation regex's in an array and then use every.
var atLeastLowerCase = /[a-z]/;
var atLeastUpperCase = /[A-Z]/;
var atLeastSpecial = /[\#\#\%\^\&\*\]\)]/;
var password = "somePass#";
var passes = [atLeast8,atLeastLowerCase,atLeastUpperCase,atLeastSpecial].every(function(a){
return a.test(password);
}) && password.length>=8;
if(passes){
//do something
}else{
//do something else
}
Pls help me with regular expression. I have method to validate password using regex:
/^(?=.*[0-9])(?=.*[!##$%^&*])[a-zA-Z0-9!##$%^&*]{6,12}$/;
I need to add to this condition that password has to contain 2 capital letters.
Thx for help!
You can add another lookahead in your regex:
/^(?=.*[0-9])(?=(?:[^A-Z]*[A-Z]){2})(?=.*[!##$%^&*])[a-zA-Z0-9!##$%^&*]{6,12}$/;
This is a really ugly way of checking password syntax. Your code would be much easier to read and debug if you split your checks into multiple steps.
For example:
/* Check for at least 2 capital letters */
if (!(/[A-Z][^A-Z]*[A-Z]/.test(password))) {
alert("Your password must contain at least two capital letters");
return false;
}
/* Check for at least 2 lower case letters */
if (!(/[a-z][^a-z]*[a-z]/.test(password))) {
alert("Your password must contain at least two lower case letters");
return false;
}
/* Check for at least one digit */
if (!(/[0-9]/.test(password))) {
alert("Your password must contain at least one digit");
return false;
}
... etc ...
I think many people have done some similar development tasks before:
I would like to check the people's email address whether only match #tomtom.com or #stream.com.
Currently, I have two solutions in my mind:
Using indexof() function
var checkTomTomEmail=eo.data.username.indexOf("#tomtom.com");
var checkStreamEmail=eo.data.username.indexOf("#stream.com");
if (checkTomTomEmail >0 || checkStreamEmail >0 )
{
//Run the login code
}
Else
{
//Please login with your tomtom or stream email
}
Using match
var patt1=/#tomtom.com/gi;
var patt2=/#stream.com/gi;
var checkTomTomEmail=eo.data.username.match(patt1);
var checkStreamEmail=eo.data.username.match(patt2);
if(indexOf(checkTomTomEmail)> 1 ||indexOf (checkStreamEmail)>1)
{
//Login
}
I still think I do not consider all the detail yet. Any suggestions?
Thanks
Perhaps if people are only allowed to enter emails for those two addresses you should only collect the username and then allow them to choose #tomtom.com or #stream.com using radiobuttons.
If you still want to go the javascript route then your regex can be combined into a single statement
var emailPatt=/#(tomtom|stream).com/gi;
if(emailPatt.test(eo.data.username))
{
//Login
}
How about this...
var emailRegex = /^([0-9a-z])+#(tomtom|stream)\.com$/ig;
if (emailRegex.test(emailRegex)) {
// Login
}
Instead of performing a .match(...) - Which you'll get a string back, we can perform a .test(...) to see if anything matches.
This pattern guarantees the following:
The "username" part of the email address must at least have a SINGLE character (For example, a#stream.com)
Username must be composed of a digit or an alphabet (Upper/Lower case - Doesn't matter because of the /i at the end)
Input must contain the entire email address without leading or tailing spaces. For example, " user#tomtom.com " will fail, it'll only accept "user#tomtom.com".)
You can customize this further by, saying, making sure username must have at least 3 characters, you can use underscore or dashes in the email address, etc.
To answer your question, both solutions won't work. Reasons:
User can enter "tom#tomtom.com Hello", and it'll pass both of your validation.
Specifically on solution #2, the dot '.' is a Regex-reserved character, it means it'll match anything, so, if the user enters " #tomtom1com", it'll pass...
More on Regex: http://www.regular-expressions.info/reference.html