Password Regular Expression

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

    Password Regular Expression

    Hi all,
    hope someone can help me,

    I am looking to do a password validation check using client side
    validation. Minimum requirement is that it must contain at least one
    number and one letter. (Ideally it would be of 6-20 characters in
    length but first things first).

    I have came up with the following regEx

    ^(\w*(?=\w+\d+) (?=\w*[a-zA-Z])\w*)$

    This works fine in IE but when testing it using NS6 it won't validate
    '1password', but will validate 'password1'. It seems the first forward
    search is consuming the first character in NS, would this be correct,
    and if so can anybody suggest either a way arround this or a different
    regular expression,

    Thanks in advance,

    SJM.

  • Evertjan.

    #2
    Re: Password Regular Expression

    Stirling wrote on 15 mei 2004 in comp.lang.javas cript:
    [color=blue]
    > I am looking to do a password validation check using client side
    > validation. Minimum requirement is that it must contain at least one
    > number and one letter. (Ideally it would be of 6-20 characters in
    > length but first things first).
    >
    > I have came up with the following regEx
    >
    > ^(\w*(?=\w+\d+) (?=\w*[a-zA-Z])\w*)$
    >
    > This works fine in IE but when testing it using NS6 it won't validate
    > '1password', but will validate 'password1'. It seems the first forward
    > search is consuming the first character in NS, would this be correct,
    > and if so can anybody suggest either a way arround this or a different
    > regular expression,
    >[/color]

    function pwTest(w){
    t1 = /^[a-z\d]{6,20}$/i // only and length
    t2 = /[a-z]/i // at least 1 letter
    t3 = /\d/ // at least 1 number

    return t1.test(w) && t2.test(w) && t3.test(w)
    }


    alert(pwTest("1 qweqweasd"))


    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)

    Comment

    • Ivo

      #3
      Re: Password Regular Expression

      "Stirling" wrote[color=blue]
      > I am looking to do a password validation check using client side
      > validation. Minimum requirement is that it must contain at least one
      > number and one letter. (Ideally it would be of 6-20 characters in
      > length but first things first).
      > <snip>[/color]

      Lookahead regexes are not very widely supported. Look at this:

      var pword='djuhy9dg yg';
      alert( /[a-z]/i.test( pword ) && /\d/.test( pword ) && /.{6,20}/.test(
      pword ) )
      // alerts true or false

      Three little regexes may not be the most efficient way, but would be fast
      enough for me.
      Ivo


      Comment

      • Lasse Reichstein Nielsen

        #4
        Re: Password Regular Expression

        Stirling <fakeEmail@NOSP AM.com> writes:
        [color=blue]
        > I am looking to do a password validation check using client side
        > validation. Minimum requirement is that it must contain at least one
        > number and one letter.[/color]
        [color=blue]
        > I have came up with the following regEx
        >
        > ^(\w*(?=\w+\d+) (?=\w*[a-zA-Z])\w*)$[/color]

        The matches a sequence of word-characters (a requirement you didn't
        mention?) that contains at least one digit(!) after a word character,
        and at least one letter. The reason you don't match "1password" is
        the first + in (?=\w+\d+).

        A simpler version could be:
        /^(?=\w*\d)(?=\w *[A-Z])\w{6,20}$/i

        Still, you are relying on lookahead, which only exists in newer
        browsers, and which IIRC is broken in IE. In many cases, making
        more than one test is much simpler than trying to encode everything
        into one RegExp.

        function testPasswd(pw) {
        return /[a-z]/i.test(pw) && /\d/.test(pw) && /^\w{6,20}*$/.test(pw);
        }
        [color=blue]
        > This works fine in IE but when testing it using NS6 it won't validate
        > '1password', but will validate 'password1'.[/color]

        That supports my recollection that lookahead in IE is broken, because
        it should not match "1password" .
        [color=blue]
        > It seems the first forward search is consuming the first character
        > in NS, would this be correct,[/color]

        Yes, because you asked it to.

        /L
        --
        Lasse Reichstein Nielsen - lrn@hotpop.com
        DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
        'Faith without judgement merely degrades the spirit divine.'

        Comment

        • Stirling

          #5
          Re: Password Regular Expression

          Evertjan. wrote:
          [color=blue]
          > Stirling wrote on 15 mei 2004 in comp.lang.javas cript:
          >
          >[color=green]
          >>I am looking to do a password validation check using client side
          >>validation. Minimum requirement is that it must contain at least one
          >>number and one letter. (Ideally it would be of 6-20 characters in
          >>length but first things first).
          >>
          >>I have came up with the following regEx
          >>
          >>^(\w*(?=\w+\d +)(?=\w*[a-zA-Z])\w*)$
          >>
          >>This works fine in IE but when testing it using NS6 it won't validate
          >>'1password' , but will validate 'password1'. It seems the first forward
          >>search is consuming the first character in NS, would this be correct,
          >>and if so can anybody suggest either a way arround this or a different
          >>regular expression,
          >>[/color]
          >
          >
          > function pwTest(w){
          > t1 = /^[a-z\d]{6,20}$/i // only and length
          > t2 = /[a-z]/i // at least 1 letter
          > t3 = /\d/ // at least 1 number
          >
          > return t1.test(w) && t2.test(w) && t3.test(w)
          > }
          >
          >
          > alert(pwTest("1 qweqweasd"))
          >
          >[/color]
          My problem is that I am using ASP.net validation, so it must consist of
          a singe validation expression. Is there any other way of doing this
          sirt of thing with a single expression without looking forward?

          SJM.

          Comment

          • Evertjan.

            #6
            Re: Password Regular Expression

            Stirling wrote on 16 mei 2004 in comp.lang.javas cript:[color=blue][color=green]
            >> function pwTest(w){
            >> t1 = /^[a-z\d]{6,20}$/i // only and length
            >> t2 = /[a-z]/i // at least 1 letter
            >> t3 = /\d/ // at least 1 number
            >>
            >> return t1.test(w) && t2.test(w) && t3.test(w)
            >> }
            >>
            >>
            >> alert(pwTest("1 qweqweasd"))
            >>
            >>[/color]
            > My problem is that I am using ASP.net validation, so it must consist of
            > a singe validation expression. Is there any other way of doing this
            > sirt of thing with a single expression without looking forward?[/color]

            This once more convinces me to stick to classical ASP.

            Either you are wrong or ASP-net stinks.

            --
            Evertjan.
            The Netherlands.
            (Please change the x'es to dots in my emailaddress)

            Comment

            • Richard Cornford

              #7
              Re: Password Regular Expression

              Evertjan. wrote:[color=blue]
              > Stirling wrote:[/color]
              <snip>[color=blue][color=green]
              >> My problem is that I am using ASP.net validation, so it must consist
              >> of a singe validation expression. Is there any other way of doing
              >> this sirt of thing with a single expression without looking forward?[/color]
              >
              > This once more convinces me to stick to classical ASP.
              >
              > Either you are wrong or ASP-net stinks.[/color]

              It does seem unlikely that ASP-net would impose such an arbitrary
              restriction, but an expression could be a function call or a function
              expression (that was called as - (function(){ ... })() -) so even with
              this restriction there would be no limit on the amount of javascript
              executed to resolve one expression.

              Richard.


              Comment

              • Stirling

                #8
                Re: Password Regular Expression

                Richard Cornford wrote:[color=blue]
                > Evertjan. wrote:
                >[color=green]
                >>Stirling wrote:[/color]
                >
                > <snip>
                >[color=green][color=darkred]
                >>>My problem is that I am using ASP.net validation, so it must consist
                >>>of a singe validation expression. Is there any other way of doing
                >>>this sirt of thing with a single expression without looking forward?[/color]
                >>
                >>This once more convinces me to stick to classical ASP.
                >>
                >>Either you are wrong or ASP-net stinks.[/color]
                >
                >
                > It does seem unlikely that ASP-net would impose such an arbitrary
                > restriction, but an expression could be a function call or a function
                > expression (that was called as - (function(){ ... })() -) so even with
                > this restriction there would be no limit on the amount of javascript
                > executed to resolve one expression.
                >
                > Richard.
                >
                >[/color]
                What I am using is the Validation controls which are part of the ASP.net
                framework (actullay I'm using a modified version called the
                DOMValidators.) These controls attach to a Textbox, and allow a regular
                expression match against the text box. Yes it is limiting, but it
                reduces the amount of javascript that needs coded quite a lot. So I'm
                stuck with using a single validation string.

                S.

                Comment

                • Jim Ley

                  #9
                  Re: Password Regular Expression

                  On Tue, 18 May 2004 19:06:10 +0100, "Richard Cornford"
                  <Richard@litote s.demon.co.uk> wrote:
                  [color=blue]
                  >Evertjan. wrote:[color=green]
                  >> Either you are wrong or ASP-net stinks.[/color]
                  >
                  >It does seem unlikely that ASP-net would impose such an arbitrary
                  >restriction,[/color]

                  It's entirely possible ASP.NET webforms stink, no seriously, they
                  really stink. They are almost certainly the worst thing ever to come
                  out of Microsoft.
                  [color=blue]
                  > but an expression could be a function call or a function
                  >expression (that was called as - (function(){ ... })() -) so even with
                  >this restriction there would be no limit on the amount of javascript
                  >executed to resolve one expression.[/color]

                  True, but stop shining the shite, there's no way it'll be worth it.

                  Jim.
                  --
                  comp.lang.javas cript FAQ - http://jibbering.com/faq/

                  Comment

                  • Richard Cornford

                    #10
                    Re: Password Regular Expression

                    Jim Ley wrote:[color=blue]
                    > "Richard Cornford" wrote:[color=green]
                    >>Evertjan. wrote:[color=darkred]
                    >>> Either you are wrong or ASP-net stinks.[/color]
                    >>
                    >>It does seem unlikely that ASP-net would impose such an
                    >>arbitrary restriction,[/color]
                    >
                    > It's entirely possible ASP.NET webforms stink, no seriously,
                    > they really stink. They are almost certainly the worst thing
                    > ever to come out of Microsoft.[/color]
                    <snip>

                    I am not sure why I gave Microsoft credit for not being that stupid,
                    they demonstrate monumental incompetence on occasions, like the fact
                    that I have to use a Mozilla/Gecko browser when I visit the MSDN site
                    because they cannot cope with the configurability of their own (IE6)
                    browser.

                    Richard.


                    Comment

                    Working...