Password Strength Regular Expression

Discussion in 'Javascript' started by pramodx, Dec 12, 2008.

  1. pramodx

    pramodx Guest

    Hi

    I just need a regular expression for the condition that the password
    has to be a combination of alphabets and numbers. It cannot be only
    numbers nor can it be only alphabets. No special characters allowed.

    Can someone please help me out with a regex for this?

    Any advice would be grateful

    Thanks,
     
    pramodx, Dec 12, 2008
    #1
    1. Advertising

  2. pramodx

    Evertjan. Guest

    pramodx wrote on 12 dec 2008 in comp.lang.javascript:

    > I just need a regular expression for the condition that the password
    > has to be a combination of alphabets and numbers. It cannot be only
    > numbers nor can it be only alphabets. No special characters allowed.
    >
    > Can someone please help me out with a regex for this?


    <script type='text/javascript'>

    var pw='a2'

    alert(/[a-z]/i.test(pw) && /\d/.test(pw) && !/[^a-z\d]/i.test(pw))

    </script>

    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)
     
    Evertjan., Dec 12, 2008
    #2
    1. Advertising

  3. pramodx wrote:
    > I just need a regular expression for the condition that the password
    > has to be a combination of alphabets and numbers. It cannot be only
    > numbers nor can it be only alphabets. No special characters allowed.
    >
    > Can someone please help me out with a regex for this?


    <http://catb.org/~esr/faqs/smart-questions.html#examples>


    PointedEars
    --
    Use any version of Microsoft Frontpage to create your site.
    (This won't prevent people from viewing your source, but no one
    will want to steal it.)
    -- from <http://www.vortex-webdesign.com/help/hidesource.htm>
     
    Thomas 'PointedEars' Lahn, Dec 12, 2008
    #3
  4. pramodx <> writes:

    > I just need a regular expression for the condition that the password
    > has to be a combination of alphabets and numbers. It cannot be only
    > numbers nor can it be only alphabets. No special characters allowed.
    >
    > Can someone please help me out with a regex for this?


    As Evertjan suggested, you should probably use more than one regexp to
    check the different requirements, instead of encoding all the
    requirements in one.

    If that's not possible, a more complex (and therefore less readable
    and less maintainable) single regexp could be
    /^(?:\d+[a-z]|[a-z]+\d)[a-z\d]*$/i

    (I notice you didn't have any requirements on the length of the
    password. If you add those, the single regexp becomes significantly
    more complex, but it's only one simple test to add outside the regexp)

    /L
    --
    Lasse Reichstein Holst Nielsen
    'Javascript frameworks is a disruptive technology'
     
    Lasse Reichstein Nielsen, Dec 12, 2008
    #4
  5. pramodx

    pramodx Guest

    Thanks Evert. It worked

    On Dec 12, 7:52 pm, "Evertjan." <> wrote:
    > pramodx wrote on 12 dec 2008 in comp.lang.javascript:
    >
    > > I just need a regular expression for the condition that thepassword
    > > has to be a combination of alphabets and numbers. It cannot be only
    > > numbers nor can it be only alphabets. No special characters allowed.

    >
    > > Can someone please help me out with a regex for this?

    >
    > <script type='text/javascript'>
    >
    > var pw='a2'
    >
    > alert(/[a-z]/i.test(pw) && /\d/.test(pw) && !/[^a-z\d]/i.test(pw))
    >
    > </script>
    >
    > --
    > Evertjan.
    > The Netherlands.
    > (Please change the x'es to dots in my emailaddress)
     
    pramodx, Dec 15, 2008
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page