Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 14 of 14
  1. #1
    New to the CF scene
    Join Date
    May 2006
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    regular expression for password

    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

  • #2
    Senior Coder
    Join Date
    Jul 2005
    Location
    New York, NY
    Posts
    1,084
    Thanks
    4
    Thanked 19 Times in 19 Posts
    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.

  • #3
    Regular Coder
    Join Date
    Oct 2003
    Location
    on a ship
    Posts
    574
    Thanks
    1
    Thanked 6 Times in 5 Posts
    this isnt a reg exp but it will do the same job.

    Code:
    <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>
    I make no attempt at pretending like I'm a professional. I offer help with what knowledge I do have.

  • #4
    Master Coder
    Join Date
    Feb 2003
    Location
    Umeå, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    RegEx in JavaScript can't handle multiple independent queries at the same time. It's easy, however, to test all three things separately.
    Code:
    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*/
    }
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #5
    New Coder
    Join Date
    Jul 2005
    Posts
    83
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by liorean View Post
    RegEx in JavaScript can't handle multiple independent queries at the same time. It's easy, however, to test all three things separately.
    Code:
    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?

  • #6
    Senior Coder
    Join Date
    Aug 2002
    Location
    UK
    Posts
    2,789
    Thanks
    2
    Thanked 14 Times in 14 Posts
    david_kw

    You were right

    Just when I thought I was getting the hang of this regEx, oh well, back to page one I go, again
    Last edited by Mr J; 11-21-2006 at 02:52 PM.
    The silent one.

    The most dangerous thing in the world is an idea.
    The most dangerous person in the world is the one with an idea.

  • #7
    Senior Coder
    Join Date
    Nov 2006
    Posts
    1,000
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

    Code:
    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

  • #8
    Senior Coder
    Join Date
    Aug 2002
    Location
    UK
    Posts
    2,789
    Thanks
    2
    Thanked 14 Times in 14 Posts
    What about this, is it acceptable coding?


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

    The silent one.

    The most dangerous thing in the world is an idea.
    The most dangerous person in the world is the one with an idea.

  • #9
    Senior Coder
    Join Date
    Jul 2005
    Location
    New York, NY
    Posts
    1,084
    Thanks
    4
    Thanked 19 Times in 19 Posts
    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.

  • #10
    Senior Coder
    Join Date
    Aug 2002
    Location
    UK
    Posts
    2,789
    Thanks
    2
    Thanked 14 Times in 14 Posts
    Beagle

    Would it be something on these lines?

    Code:
    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)
    
    }
    }
    The silent one.

    The most dangerous thing in the world is an idea.
    The most dangerous person in the world is the one with an idea.

  • #11
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,318
    Thanks
    203
    Thanked 2,566 Times in 2,544 Posts
    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")
    }
    }

  • #12
    New Coder
    Join Date
    Jul 2007
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #13
    Senior Coder
    Join Date
    Aug 2002
    Location
    UK
    Posts
    2,789
    Thanks
    2
    Thanked 14 Times in 14 Posts
    See if this works

    Code:
    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")
    }
    The silent one.

    The most dangerous thing in the world is an idea.
    The most dangerous person in the world is the one with an idea.

  • #14
    New Coder
    Join Date
    Jul 2007
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks Mr J


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •