Regular Expression to validate password

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • adzir

    Regular Expression to validate password

    Hi,

    I need to validate password keyed in by the system users so that the
    password will contain only letters and numbers plus at least one
    capital letter. Exclude these symbols

    , < ? / * ( ) & ^ % $ # ! ~ ` " '

    this is my code:

    ereg("[A-Za-z0-9[^\,\<\>\?\/\*\(\)\&\^\%\$\ #\!\~\`\"\']]+$", $str)

    am i correct?

  • Michael Fesser

    #2
    Re: Regular Expression to validate password

    ..oO(adzir)
    >I need to validate password keyed in by the system users so that the
    >password will contain only letters and numbers plus at least one
    >capital letter. Exclude these symbols
    >
    >, < ? / * ( ) & ^ % $ # ! ~ ` " '
    >
    >this is my code:
    >
    >ereg("[A-Za-z0-9[^\,\<\>\?\/\*\(\)\&\^\%\$\ #\!\~\`\"\']]+$", $str)
    >
    >am i correct?
    Way too complicated and using the old deprecated ereg* functions.

    If the PW should only contain letters and digits, then use a pattern
    that matches for exactly that:

    preg_match('#[A-Z\d]+#i', $str);

    If there has to be a capital letter somewhere in the PW, then try a
    combination like this:

    preg_match('#[a-zA-Z\d]*[A-Z][a-zA-Z\d]*#', $str);

    HTH
    Micha

    Comment

    • adzir

      #3
      Re: Regular Expression to validate password

      On May 7, 11:53 pm, Michael Fesser <neti...@gmx.de wrote
      Way too complicated and using the old deprecated ereg* functions.
      >
      If the PW should only contain letters and digits, then use a pattern
      that matches for exactly that:
      >
      preg_match('#[A-Z\d]+#i', $str);
      >
      If there has to be a capital letter somewhere in the PW, then try a
      combination like this:
      >
      preg_match('#[a-zA-Z\d]*[A-Z][a-zA-Z\d]*#', $str);
      >
      HTH
      Micha
      Hey thanks,

      I was referring an old book. I didn't noticed that there is another
      function best suits to my needs. My broadband was out for a week and I
      was stuck dead.

      but u didn't answer my another question which was to get rid of the
      symbols? anyway thanks a lot

      Comment

      • Daz

        #4
        Re: Regular Expression to validate password

        On May 7, 5:08 pm, adzir <muad...@yahoo. comwrote:
        On May 7, 11:53 pm, Michael Fesser <neti...@gmx.de wrote
        >
        Way too complicated and using the old deprecated ereg* functions.
        >
        If the PW should only contain letters and digits, then use a pattern
        that matches for exactly that:
        >
        preg_match('#[A-Z\d]+#i', $str);
        >
        If there has to be a capital letter somewhere in the PW, then try a
        combination like this:
        >
        preg_match('#[a-zA-Z\d]*[A-Z][a-zA-Z\d]*#', $str);
        >
        HTH
        Micha
        >
        Hey thanks,
        >
        I was referring an old book. I didn't noticed that there is another
        function best suits to my needs. My broadband was out for a week and I
        was stuck dead.
        >
        but u didn't answer my another question which was to get rid of the
        symbols? anyway thanks a lot
        I didn't notice that there was another question. In any case, check
        out strtr(), or preg_replace().

        Comment

        • Michael Fesser

          #5
          Re: Regular Expression to validate password

          ..oO(adzir)
          >but u didn't answer my another question which was to get rid of the
          >symbols? anyway thanks a lot
          You don't have to. Just match on the allowed characters. If someone
          enters a $, # or whatever, the matching will fail.

          Micha

          Comment

          • gosha bine

            #6
            Re: Regular Expression to validate password

            Michael Fesser wrote:
            .oO(adzir)
            >
            >I need to validate password keyed in by the system users so that the
            >password will contain only letters and numbers plus at least one
            >capital letter. Exclude these symbols
            >>
            >, < ? / * ( ) & ^ % $ # ! ~ ` " '
            >>
            >this is my code:
            >>
            >ereg("[A-Za-z0-9[^\,\<\>\?\/\*\(\)\&\^\%\$\ #\!\~\`\"\']]+$", $str)
            >>
            >am i correct?
            >
            Way too complicated and using the old deprecated ereg* functions.
            >
            If the PW should only contain letters and digits, then use a pattern
            that matches for exactly that:
            >
            preg_match('#[A-Z\d]+#i', $str);
            >
            If there has to be a capital letter somewhere in the PW, then try a
            combination like this:
            >
            preg_match('#[a-zA-Z\d]*[A-Z][a-zA-Z\d]*#', $str);
            >
            HTH
            Micha
            Michael, your pattern will also match "*&(*%& Z #)&*(", because it's not
            anchored. Use ^ and $ (and not forget /D) to make it do what you want.

            --
            gosha bine

            extended php parser ~ http://code.google.com/p/pihipi
            blok ~ http://www.tagarga.com/blok

            Comment

            • Michael Fesser

              #7
              Re: Regular Expression to validate password

              ..oO(gosha bine)
              >Michael Fesser wrote:
              >
              >preg_match(' #[a-zA-Z\d]*[A-Z][a-zA-Z\d]*#', $str);
              >>
              >Michael, your pattern will also match "*&(*%& Z #)&*(", because it's not
              >anchored. Use ^ and $ (and not forget /D) to make it do what you want.
              Correct. Good point.

              Micha

              Comment

              • adzir

                #8
                Re: Regular Expression to validate password

                On May 8, 1:31 am, Michael Fesser <neti...@gmx.de wrote:
                .oO(gosha bine)
                >
                Michael Fesser wrote:
                >
                preg_match('#[a-zA-Z\d]*[A-Z][a-zA-Z\d]*#', $str);
                >
                Michael, your pattern will also match "*&(*%& Z #)&*(", because it's not
                anchored. Use ^ and $ (and not forget /D) to make it do what you want.
                >
                Correct. Good point.
                >
                Micha
                Where can I find a good site to explain to me all the syntax (simbols,
                characters used in the preg_match function)?

                php.net seems not to satisfy me.

                Comment

                • Gordon Burditt

                  #9
                  Re: Regular Expression to validate password

                  >I need to validate password keyed in by the system users so that the
                  >password will contain only letters and numbers plus at least one
                  >capital letter.
                  Gee, many administrators would like to PROHIBIT such passwords on the
                  grounds that they are "too obvious".

                  Comment

                  • Jerry Stuckle

                    #10
                    Re: Regular Expression to validate password

                    adzir wrote:
                    On May 8, 1:31 am, Michael Fesser <neti...@gmx.de wrote:
                    >.oO(gosha bine)
                    >>
                    >>Michael Fesser wrote:
                    >>>preg_match(' #[a-zA-Z\d]*[A-Z][a-zA-Z\d]*#', $str);
                    >>Michael, your pattern will also match "*&(*%& Z #)&*(", because it's not
                    >>anchored. Use ^ and $ (and not forget /D) to make it do what you want.
                    >Correct. Good point.
                    >>
                    >Micha
                    >
                    Where can I find a good site to explain to me all the syntax (simbols,
                    characters used in the preg_match function)?
                    >
                    php.net seems not to satisfy me.
                    >
                    Just google for "Regular expressions". There are thousands of them out
                    there, offering all levels of explanations. Many are pretty good.

                    --
                    =============== ===
                    Remove the "x" from my email address
                    Jerry Stuckle
                    JDS Computer Training Corp.
                    jstucklex@attgl obal.net
                    =============== ===

                    Comment

                    • Daz

                      #11
                      Re: Regular Expression to validate password

                      On May 11, 4:05 am, adzir <muad...@yahoo. comwrote:
                      On May 8, 1:31 am, Michael Fesser <neti...@gmx.de wrote:
                      >
                      .oO(gosha bine)
                      >
                      >Michael Fesser wrote:
                      >
                      >preg_match(' #[a-zA-Z\d]*[A-Z][a-zA-Z\d]*#', $str);
                      >
                      >Michael, your pattern will also match "*&(*%& Z #)&*(", because it's not
                      >anchored. Use ^ and $ (and not forget /D) to make it do what you want.
                      >
                      Correct. Good point.
                      >
                      Micha
                      >
                      Where can I find a good site to explain to me all the syntax (simbols,
                      characters used in the preg_match function)?
                      >
                      php.net seems not to satisfy me.
                      www.regular-expressions.info is a good place to start. :)

                      Comment

                      • Schraalhans Keukenmeester

                        #12
                        Re: Regular Expression to validate password

                        At Thu, 10 May 2007 20:05:32 -0700, adzir let his monkeys type:
                        On May 8, 1:31 am, Michael Fesser <neti...@gmx.de wrote:
                        >.oO(gosha bine)
                        >>
                        >Michael Fesser wrote:
                        >>
                        >preg_match(' #[a-zA-Z\d]*[A-Z][a-zA-Z\d]*#', $str);
                        >>
                        >Michael, your pattern will also match "*&(*%& Z #)&*(", because it's not
                        >anchored. Use ^ and $ (and not forget /D) to make it do what you want.
                        >>
                        >Correct. Good point.
                        >>
                        >Micha
                        >
                        Where can I find a good site to explain to me all the syntax (simbols,
                        characters used in the preg_match function)?
                        >
                        php.net seems not to satisfy me.
                        Kudos! Now *that's* the question so many fail to ask. The detours people
                        will take just to avoid learning the ropes with regex, unbelievable.

                        It _has_ a fairly steep learning curve and a level of abstraction that
                        requires concentration and focus, but the merits are well worth the effort
                        imho.

                        One catch (from my own experience): try to keep an open mind as to
                        what solution is needed in a given situation. Some code is full of regex
                        fixing stuff that could (should?!) easily be dealt with using normal
                        string functions. Often easier to follow/adapt (for you and future editors
                        of your code, good documentation explaining what your regex does is key.)
                        and less resource-hungry. A complex regular expression can be quite taxing
                        on your server.

                        The 'best' excuse found here to avoid the matter in the NG undoubtedly
                        was: "I don't trust regex!".

                        There are several sites that offer, besides explanation, on- or offline
                        utilities allowing you to experiment with regex on the fly. Be aware of
                        the subtle dialect differences between different regex flavours though. It
                        may prevent you from going ape at some point...

                        Good luck learning regex. You'll soon reach the point where you start
                        wondering how you ever managed without.

                        Sh.

                        Comment

                        • -Lost

                          #13
                          Re: Regular Expression to validate password

                          adzir wrote:
                          On May 8, 1:31 am, Michael Fesser <neti...@gmx.de wrote:
                          >.oO(gosha bine)
                          >>
                          >>Michael Fesser wrote:
                          >>>preg_match(' #[a-zA-Z\d]*[A-Z][a-zA-Z\d]*#', $str);
                          >>Michael, your pattern will also match "*&(*%& Z #)&*(", because it's not
                          >>anchored. Use ^ and $ (and not forget /D) to make it do what you want.
                          >Correct. Good point.
                          >>
                          >
                          Where can I find a good site to explain to me all the syntax (simbols,
                          characters used in the preg_match function)?
                          >
                          php.net seems not to satisfy me.


                          --
                          -Lost
                          Remove the extra words to reply by e-mail. Don't e-mail me. I am
                          kidding. No I am not.

                          Comment

                          Working...