...

View Full Version : regular expression for password



swarnaprava
11-02-2006, 10:39 AM
I want a regular expression for a string satisfying the following conditions

1)The length should be minimum 6 characters.
2) It should contain atleast one number
3) It sould contain atleast one character

Any help on this will be really appreciated.

Thanks

Beagle
11-02-2006, 04:13 PM
Hmm, I failed to come up with one quickly, here's several to do what you want.

When you say character, do you mean alphabet character, or any character.
If you meant ANY character (like @) then below replace [A-z] with \D

/.{6,}/ (minimum 6 chars)
/^.*((\d*.*[A-z]*)|([A-z]*.*\d*)).*$/ (contains a number and a letter in any order)

I'm not the most amazing regex guy, and I'm really rushed, so that's my answer for now.

brandonH
11-02-2006, 11:52 PM
this isnt a reg exp but it will do the same job.



<html>
<head><</head>
<body>
<input type=password id=pass>
<input type=button value='check pass' onclick="validatePass();">

</body>

<script type=text/javascript>

function validatePass(){
var input=document.getElementById('pass');
if(input.value.length<'6'){alert('your password must be at least 6 characters long');return;}
else{
var num="0123456789";
var sym="!@#$%^&_";
var numblank='';
var symblank='';
i='0';
j='0';
while(j<input.value.length){
if(input.value.charAt(j)==num.charAt(i)){j='0';i='0';numblank='1';break;}
else{i++;}
if(i==num.length-(1)){j++;i=0;}
}

while(j<input.value.length){
if(input.value.charAt(j)==sym.charAt(i)){j='0';i='0';symblank='1';break;}
else{i++;}
if(i==sym.length-(1)){j++;i=0;}
}

if(numblank==''){alert('there are no numbers in your password');}
if(symblank==''){alert('there are no special characters in your password');}

}
}
</script>

</html>

liorean
11-07-2006, 02:22 PM
RegEx in JavaScript can't handle multiple independent queries at the same time. It's easy, however, to test all three things separately.
if( (str.length>=6) && /\d/.test(str) && /[a-z]/i.test(str) ){
/*things to do if the password is 6 or more characters,
contain at least one digit and at least one letter*/
}

sushestvo
11-20-2006, 10:03 PM
RegEx in JavaScript can't handle multiple independent queries at the same time. It's easy, however, to test all three things separately.
if( (str.length>=6) && /\d/.test(str) && /[a-z]/i.test(str) ){
/*things to do if the password is 6 or more characters,
contain at least one digit and at least one letter*/
}

Could someone please modify this so it checks for password to have at least 2 letters and 2 numbers and be 7 characters long (alphanumeric).
I understand length str.length>=7, but what about minimum numbers, letters requirement?

Mr J
11-20-2006, 11:36 PM
david_kw

You were right :thumbsup:

Just when I thought I was getting the hang of this regEx, oh well, back to page one I go, again :(

david_kw
11-21-2006, 09:02 AM
I make zero claims on being any good at regex, but wouldn't that only work for 2 digits in a row and 2 letter in a row?

This is what I was thinking.


if( (str.length==7) && /\d.*\d/.test(str) && /[a-z].*[a-z]/i.test(str) ){

/*things to do if the password is 6 or more characters,
contain at least one digit and at least one letter*/
}


david_kw

Mr J
11-21-2006, 03:06 PM
What about this, is it acceptable coding?



if( (str.length==7) &&str.replace(/[^0-9]/g,'').length>=2&&str.replace(/[^a-z]/g,'').length>=2 ){
/*things to do if the password is 6 or more characters,
contain at least one digit and at least one letter*/
}

Beagle
11-22-2006, 03:47 AM
no reason to do the str.replace, str.match will return an array of matches, so you can match on \d and take the length of that.

Mr J
11-22-2006, 03:11 PM
Beagle

Would it be something on these lines?


if(str.length==7){
if(str.match(/\d/g)!=null&&str.match(/\d/g).length>=2&&str.match(/[a-z]/gi)!=null&&str.match(/[a-z]/gi).length>=2 ){

alert("At least 2 numbers of which there are "+str.match(/\d/g).length+"\n\nAt least 2 letters of which there are "+str.match(/[a-z]/gi).length)

}
}

Philip M
11-22-2006, 07:15 PM
Mr J - I have tested the following which seems to work OK:-

function testpw(lstr) {

if (lstr.value.length==7) {
if (lstr.value.match (/\d/g)!=null && lstr.value.match(/\d/g).length>=2 && lstr.value.match (/[a-z]/gi)!=null && lstr.value.match (/[a-z]/gi).length>=2 ) {
alert ("Password is valid")
}
else {
alert ("Password is not valid")
}
}

undertaker78
07-24-2007, 05:43 AM
I have never used regular expression, therefore if any one can help me to have a regular expression for a string with the following conditions
1)The length should be minimum 6 characters but with no maximum.
2) It should contain atleast three alphabet characters


I really appreciate if anyone can help me with this (with explanation if possible).

Thanks

Mr J
07-24-2007, 03:28 PM
See if this works



str="123abc"

re=/\w{6,}/

if(re.test(str)&&str.match(/[a-z]/gi)!=null&&str.match(/[a-z]/gi).length>=3){
alert("Y")
}
else{
alert("N")
}

undertaker78
07-25-2007, 08:02 PM
Thanks Mr J :thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum