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 ...
Related
ı 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.
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
}
I'm using a regex below to validate password to accept alphanumeric characters only. The regex works if I enter 2 characters one alpha and one number but if more than two characters my regex doesn't work. I want if possible the following results as shown in "Expected Behavior". Can anyone help me rewrite my regex?
JavaScript
function checkPasswordComplexity(pwd) {
var regularExpression = /^[a-zA-Z][0-9]$/;
var valid = regularExpression.test(pwd);
return valid;
}
Current Behavior
Password:Valid
a1:true
aa1:false
aa11:false
Expected Behavior
Password:Valid
aa:false (should have at least 1 number)
1111111:false (should have at least 1 letter)
aa1:true
aa11:true
a1a1a1a1111:true
You want to add "one or more", you're currently checking for a letter followed by a number.
Try:
/^[a-zA-Z0-9]+$/
+ means 'one or more'
I also joined the ranges.
Note: I don't understand why you'd want to limit the password to such a small range though, having a wide character range will make your passwords stronger.
Here is a fiddle demonstrating the correct behavior
If you just want to validate that the password has at least one letter and at least one number, you can check like this:
function checkPasswordComplexity(pwd) {
var letter = /[a-zA-Z]/;
var number = /[0-9]/;
var valid = number.test(pwd) && letter.test(pwd); //match a letter _and_ a number
return valid;
}
function checkPasswordComplexity(pwd) {
var regularExpression = /^(?=.*[0-9])(?=.*[a-zA-Z])([a-zA-Z0-9]+)$/;
var valid = regularExpression.test(pwd);
return valid;
}
You can use this:
/^(?=.*\d)(?=.*[a-z])[a-z\d]{2,}$/i
Try doing this:
var regularExpression = /^[a-zA-Z0-9]+$/;
This means "one or more letter or number."
However, some users might also want to enter symbols (like &*#) in their passwords. If you just want to make sure there is at least one letter and number while still allowing symbols, try something like this:
var regularExpression = /^(?=.*[a-zA-Z])(?=.*[0-9]).+$/;
The (?=.*[a-zA-Z]) is a positive lookahead. This means that it makes sure that there is a letter ahead of it, but it doesn't affect the regex.
{
var pwd=document.getElementById('pwd').value;
var reg = /^[a-zA-Z0-9]{8,}$/;
var re=reg.test(pwd);
alert(re);
}
I think lookaround aren't supported by javascript, so you can use:
^([a-zA-Z]+\d+)|(\d+[a-zA-Z]+)
But if they are supported:
/^(?=.*\d)(?=.*[a-zA-Z])[a-zA-Z\d]{2,}$/
I am working with an email validation script and all is well, apart from if a user tries to enter an address with only two characters in the domain such as test#me.com or temp#ip.com
The validation then fires an error, I have looked through but cant see where this behaviour is being targeted, the code is below...
function validate_youremail()
{
var isvalidemailflag = 0;
if(jQuery("#property_mail_email").val() == '')
{
isvalidemailflag = 1;
}else
if(jQuery("#property_mail_email").val() != '')
{
var a = jQuery("#property_mail_email").val();
var filter = /^[a-zA-Z0-9]+[a-zA-Z0-9_.-]+[a-zA-Z0-9_-]+#[a-zA-Z0-9]+[a-zA-Z0-9.-]+[a-zA-Z0-9]+.[a-z]{2,4}$/;
//if it's valid email
if(filter.test(a)){
isvalidemailflag = 0;
}else{
isvalidemailflag = 1;
}
}
if(isvalidemailflag)
{
youremail.addClass("error");
youremailInfo.text("Please Enter valid Email Address");
youremailInfo.addClass("message_error2");
return false;
}else
{
youremail.removeClass("error");
youremailInfo.text("");
youremailInfo.removeClass("message_error");
return true;
}
Its probably staring me straight in the face but its been a long day :) Can anyone point me in the right direction?
#[a-zA-Z0-9]+[a-zA-Z0-9.-]+[a-zA-Z0-9]+
Means "#" then "one or more of those characters" then "one or more of those characters and dots and hyphens" then "one or more of those characters".
That makes it "at least three characters".
You probably want to change the middle part (of that snippet) to be zero or more (i.e. * instead of +).
The expression is still broken though. The problem that jumps out at me is that it rejects email addresses with a + in the part before the #.
Email Validation as per RFC2822 standards.
Pattern: /[a-z0-9!#$%&'*+/=?^_{|}~-]+(?:.[a-z0-9!#$%&'*+/=?^_{|}~-]+)*#(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/g
Source: RegExr
Mind you, the RFC2822 standard doesn't allow upper case characters in an email address, but you can easily adapt it for your own purposes.
I recommend you to use another regular expression.
This regular expression has been extracted from the PHP source code written in C.
/^(?!(?:(?:\x22?\x5C[\x00-\x7E]\x22?)|(?:\x22?[^\x5C\x22]\x22?)){255,})(?!(?:(?:\x22?\x5C[\x00-\x7E]\x22?)|(?:\x22?[^\x5C\x22]\x22?)){65,}#)(?:(?:[\x21\x23-\x27\x2A\x2B\x2D\x2F-\x39\x3D\x3F\x5E-\x7E]+)|(?:\x22(?:[\x01-\x08\x0B\x0C\x0E-\x1F\x21\x23-\x5B\x5D-\x7F]|(?:\x5C[\x00-\x7F]))*\x22))(?:\.(?:(?:[\x21\x23-\x27\x2A\x2B\x2D\x2F-\x39\x3D\x3F\x5E-\x7E]+)|(?:\x22(?:[\x01-\x08\x0B\x0C\x0E-\x1F\x21\x23-\x5B\x5D-\x7F]|(?:\x5C[\x00-\x7F]))*\x22)))*#(?:(?:(?!.*[^.]{64,})(?:(?:(?:xn--)?[a-z0-9]+(?:-+[a-z0-9]+)*\.){1,126}){1,}(?:(?:[a-z][a-z0-9]*)|(?:(?:xn--)[a-z0-9]+))(?:-+[a-z0-9]+)*)|(?:\[(?:(?:IPv6:(?:(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){7})|(?:(?!(?:.*[a-f0-9][:\]]){7,})(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,5})?::(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,5})?)))|(?:(?:IPv6:(?:(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){5}:)|(?:(?!(?:.*[a-f0-9]:){5,})(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,3})?::(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,3}:)?)))?(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9]{2})|(?:[1-9]?[0-9]))(?:\.(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9]{2})|(?:[1-9]?[0-9]))){3}))\]))$/i
.test('temp#ip.com');