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
}
Related
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.
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 am working to validate a string of email addresses. This pattern works fine if there is only one email address:
var pattern = /^\w+#[a-zA-Z_]+?\.[a-zA-Z]{2,3}$/;
But if I have two email addresses separated by space or by a newline, then it does not validate. For example:
xyz#abc.com xyz#bbc.com
or
xyz#abc.com
xyz#bbc.com
Can you please tell me what would be a way to do it? I am new to regular expressions.
Help much appreciated! Thanks.
Try this RegEx
/^\s*(?:\w+#[a-zA-Z_]+?\.[a-zA-Z]{2,3}\b\s*)+$/
In the above image, everything inside Group 1 is what you already had. I have added a word ending and spaces.
It will match "xyz#abc.com", " xyz#bbc.com ", "xyz#abc.com xyz#bbc.com" and email addresses in multiple lines also.
Update
I got the RegEx for Email from http://www.regular-expressions.info/email.html and I have used it in my expression. You can find it below:
/^\s*(?:([A-Z0-9._%+-]+#[A-Z0-9.-]+\.[A-Z]{2,4})\b\s*)+$/i
Change the ^ and $ anchors to word boundaries, \b.
/\b\w+...{2,3}\b/
You should also note that the actual specification for email addresses is extremely complicated and there are many emails that will fail this test -- for example those with multiple periods in the domain. May be okay for your purposes, but just pointing it out.
try this
function validateEmail(field) {
var regex=/\b[A-Z0-9._%+-]+#[A-Z0-9.-]+\.[A-Z]{2,4}\b/i;
return (regex.test(field)) ? true : false;
}
function validateMultipleEmailsCommaSeparated(value) {
var result = value.split(" ");
for(var i = 0;i < result.length;i++)
if(!validateEmail(result[i]))
return false;
return true;
}
You might consider simply splitting the whole string into an actual array of email addresses, instead of trying to validate the entire thing at once. This has the advantage of allowing you to point out in your validation message which address failed.
uld look like this:
var emailRegex = /^[A-Z0-9._%+-]+#(?:[A-Z0-9-]+\.)+[A-Z]{2,4}$/i; // http://www.regular-expressions.info/email.html
var split = form.emails.value.split(/[\s;,]+/); // split on any combination of whitespace, comma, or semi-colon
for(i in split)
{
email = split[i];
if(!emailRegex.test(email))
{
errMsg += "The to e-mail address ("+email+") is invalid.\n";
}
}
Your best regular expression for multiple emails accepts all special characters
(-*/+;.,<>}{[]||+_!##$%^&*())
Best Regular Expression for multiple emails
/^([A-Z0-9.%+-]+#[A-Z0-9.-]+.[A-Z]{2,6})*([,;][\s]*([A-Z0-9.%+-]+#[A-Z0-9.-]+.[A-Z]{2,6}))*$/i
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 use following code to check if a user input is lowercase or not. I will allow characters from a to z. no other characters allowed.
JavaScript file:
var pat = /[a-z]/;
function checkname()
{
var t = $("input[name='user_name']").val();
if(pat.test(t) == false)
{
alert('Only lowercase characters allowed');
}
}
//... other functions
But this donot work all the time. If I enter industrialS, it will not find that capital 'S'.
I also tried: /^[a-z]$/ and /[a-z]+/. But not working.
PLease help me.
Your regular expression just checks to see if the string has any lower-case characters. Try this:
var pat = /^[a-z]+$/;
That pattern will only match strings that have one or more lower-case alphabetic characters, and no other characters. The "^" at the beginning and the "$" at the end are anchors that match the beginning and end of the tested string.
if((/[a-z]/.test(email))==true){//allow the small characters}
Your regexp should be:
/^[a-z]+$/
Since all you want is lower case letters, instead of just telling the user s/he's done something wrong, I would fix it:
function checkname() {
var disallowed = /[^a-z]/gi; // g=global , i=case-insensitive
if (this.value == disallowed) {
//delete disallowed characters
this.value = this.value.replace(disallowed,'');
alert('Only lowercase letters allowed');
//instead of an alert, i would use a less intrusive fadeIn() message
}
this.value = this.value.toLowerCase();
}