validate password..

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • vikas1111
    New Member
    • Feb 2008
    • 122

    validate password..

    Hi All

    I Want to validate password,, the condition is that first 2 letters must be numbers and remaining 4 must be alphabets..How can i solve this ????
  • rizwan6feb
    New Member
    • Jul 2007
    • 108

    #2
    This could be 1 of many solutions
    Code:
        $password="13abcc";
        if(strlen($password)>=6){
            $number_part=substr($password,0,2);
            $alpha_part=substr($password,2,4);
            if(ereg("[^0-9]",$number_part)){
                $msg="First two characters must be numbers";
            }
            else if(ereg("[^a-zA-Z]",$alpha_part)){
                $msg="Last four characters must be alphabets!";
            }
            else
                $msg="Password is valid!";
        }
        echo $msg;

    Comment

    • hsriat
      Recognized Expert Top Contributor
      • Jan 2008
      • 1653

      #3
      Originally posted by vikas1111
      Hi All

      I Want to validate password,, the condition is that first 2 letters must be numbers and remaining 4 must be alphabets..How can i solve this ????
      Or use preg_match with this regular expression.
      "/^[0-9]{2}[a-zA-Z]{4}$/"

      Comment

      • vikas1111
        New Member
        • Feb 2008
        • 122

        #4
        Originally posted by hsriat
        Or use preg_match with this regular expression.
        "/^[0-9]{2}[a-zA-Z]{4}$/"


        Thanks guys...

        I am donig it in javascript so i used the above regular exp.

        Thanks for both of you..

        Comment

        • hsriat
          Recognized Expert Top Contributor
          • Jan 2008
          • 1653

          #5
          Originally posted by vikas1111
          I am donig it in javascript so i used the above regular exp.
          and you asked this question in PHP forum.

          Comment

          • vikas1111
            New Member
            • Feb 2008
            • 122

            #6
            Originally posted by hsriat
            and you asked this question in PHP forum.

            By the way in am actually working in php,, I had to validate a form,, I was looking for reg expr..

            Comment

            • Atli
              Recognized Expert Expert
              • Nov 2006
              • 5062

              #7
              Originally posted by vikas1111
              I am donig it in javascript so i used the above regular exp.
              Be careful tho. Don't assume the value PHP gets is valid just because Javascript validated it.
              It can be easily bypassed and invalid, even malicious, passwords sent.

              Comment

              • TheServant
                Recognized Expert Top Contributor
                • Feb 2008
                • 1168

                #8
                It is best to use both PHP and javascript. have javascript test for invalid characters and if all fields are filled in (front end) and then where ever the form goes you NEED PHP checking to make sure that the input is sanitized.

                Basically that way all the javascript is for is to reduce the serverload when normal users (those with javascript enabled) make a mistake filling in the form. The 0.01% of users who purposefully enter in bad data need to be dealt with via PHP validation and sanitation.

                Again, I strongly recommend you do it this way as javascript alone is useless for safety.

                Comment

                Working...