regex for password validation - convert PHP regex to JavaScript regex

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kummu4help
    New Member
    • Nov 2008
    • 14

    regex for password validation - convert PHP regex to JavaScript regex

    can anyone give me a regex to validate the password with following conditions

    1. password should have atleast one alphabet and atleast one digit.
    2.password should be atleast 6 characters and maximum of 8.
    3. also password can allow specialcharacte rs but it is not mandatory.
    without spl.characters also password is valid but it should have atleast one alphabet and minimum 1 digit.
    hope i am clear.
    i tried with ctype_alnum() function in php but it is accepting if all characters or either alphabet or digit. but i want to enforce atleast one alphabet and one digit should be in password

    can any one give me a regex for this pls.....
  • kummu4help
    New Member
    • Nov 2008
    • 14

    #2
    at present i am trying with the following code
    function isAlphaNumeric( $sInput)
    {
    if(false===(pre g_match("/[a-zA-Z]/", $sInput) > 0))
    {
    return false;
    }
    if(false===(pre g_match("/[0-9]/", $sInput) > 0))
    {
    return false;
    }
    return true;
    }
    can anyone improve it to suite my requirements which i specified above

    Comment

    • kummu4help
      New Member
      • Nov 2008
      • 14

      #3
      now i am trying with the following
      Code:
          if(false===(preg_match("/^(?=.*\d)(?=.*[a-z]).{6,8}$/", $sInput) > 0))
      	{
      		return false;
      	}
      it's working fine.
      but i want to use the same expression in javascript also for client side validation

      can any one tell me how i can use the same regular expression in javascript.
      i am trying with following but it's not working
      Code:
      	var regex=/\(?=.*\d\)\(?=.*[a-z]\).{6,8}/;
      	if(regex.test(pword))
      	{
      		alert("valid password");
      	}
              else
              {
                        alert("invalid password");
               }

      Comment

      • kummu4help
        New Member
        • Nov 2008
        • 14

        #4
        convert php regex to javascript regex

        Hi
        i have the following regex in php and it's working fine for me
        Code:
        if(false===(preg_match("/^(?=.*\d)(?=.*[a-z]).{6,8}$/", $sInput) > 0))
        {
        return false;
        }
        but i want to use the same regex in javascript for the sake of client side validation
        can anyone tell how i can use the same regex in java script
        the regex should test the following in a password
        1. password should contain atleast one numeric and one alphabet
        2.password should be in between 6 to 8 characters
        i am trying with the following but it's not working.
        Code:
        var regex=/\(?=.*\d\)\(?=.*[a-z]\).{6,8}/;
        if(regex.test(pword))
        {
        alert("valid");
        }
        else
        {
        alert("invalid");
        }
        Last edited by acoder; Dec 1 '08, 02:04 PM. Reason: changed quote to code tags

        Comment

        • anijos
          New Member
          • Nov 2008
          • 52

          #5
          try this

          var regex = /(?=.*\d)(?=.*[a-z]).{6,8}/;

          Comment

          • Markus
            Recognized Expert Expert
            • Jun 2007
            • 6092

            #6
            First of all, kummu4help, we do not just happily 'hand out' code to anyone who asks for it. You have to show effort and understanding for us to help you.

            Secondly, this is the PHP forum. If you're having problems with javascript, then you should go to javascript and ask.

            Moderator.

            Comment

            • acoder
              Recognized Expert MVP
              • Nov 2006
              • 16032

              #7
              JavaScript doesn't support atomic grouping - see Regular Expressions JavaScript.

              So you'll have to try to find a combination that could work in one go or use two separate tests.

              Comment

              • kummu4help
                New Member
                • Nov 2008
                • 14

                #8
                Hi acoder,
                the following regex was working fine for me in javascript.
                i forgot to place the $ sign at the end

                var regex=/\(?=.*\d\)\(?=. *[a-z]\).{6,8}$/;
                if(regex.test(p word))
                {
                alert("valid");
                }
                else
                {
                alert("invalid" );
                }
                now i want to improve this so that it can work for caseinsensitive also.
                but i don't know where to put the i (case insensitive) in the above regex
                can u help me please....

                Comment

                • acoder
                  Recognized Expert MVP
                  • Nov 2006
                  • 16032

                  #9
                  You also need an opening caret ^ at the beginning. For case-insensitive matches, use the i modifier, e.g. /^[a-z]+/i

                  Comment

                  • acoder
                    Recognized Expert MVP
                    • Nov 2006
                    • 16032

                    #10
                    Merged threads since they're on the same topic.

                    Moderator.

                    Comment

                    Working...