Simple Regular Expression (apparently)

Last post 05-04-2007 6:31 AM by chandana. 7 replies.

Sort Posts:

  • Simple Regular Expression (apparently)

    05-03-2007, 4:04 PM
    • Loading...
    • dotnetuk
    • Joined on 07-28-2004, 11:19 AM
    • Posts 16

    Hi,

    I've been trying to write a very simple Regular Expression for a password textbox, which consists of restricting the password length to between 4 and 20 characters, and having no whitespace.

    However, I can't find any easy way of doing this (I would like to have the whitespaces checked within the Regular Expression rather than using another method.

    Is there an easy way of doing this?  I have tried using the [^\s] character class along with \w{4,20} but it doesn't work,

    Thanks for your time,

    John

  • Re: Simple Regular Expression (apparently)

    05-03-2007, 4:20 PM
    Answer
    • Loading...
    • bullpit
    • Joined on 06-29-2006, 3:59 PM
    • Posts 3,734

    ^(?=.*[a-zA-Z].*[a-zA-Z])(?=.*\d.*\d)[a-zA-Z0-9]{5,20}$

    Description: Password matching expression. Password must consists of at least 5 characters and not more than 20 characters. Password must contain only letters and digits. Password must contain at least 2 digits and at least 2 letters.

    Also, try www.regexlib.com

    Enter "password" as search criteria and you will get more examples.

     

    Thanks,
    Max
    If you can read this, you are too close to the screen!!!

    Get instant help for your problems here!
  • Re: Simple Regular Expression (apparently)

    05-03-2007, 4:23 PM
    • Loading...
    • haoest
    • Joined on 10-25-2005, 8:20 PM
    • Posts 403

    Just wondering why is it wrong to contain space in the password field?

    The only concern is you don't know if .net will try to trim it when the space occurs on either end of the string.

     

    Debugger is my best friend. (http://haoest.info)
  • Re: Simple Regular Expression (apparently)

    05-03-2007, 4:43 PM
    • Loading...
    • Haissam
    • Joined on 10-05-2006, 2:25 AM
    • Beirut - Lebanon
    • Posts 5,293

    Check below expression

    ^[a-zA-Z0-9]{4,20}

    HC

    Haissam Abdul Malak
    MCAD.NET
    | Blog |
  • Re: Simple Regular Expression (apparently)

    05-03-2007, 9:33 PM
    • Loading...
    • mokeefe
    • Joined on 08-20-2006, 5:15 AM
    • Canberra Australia
    • Posts 2,096
    Haissam:

    ^[a-zA-Z0-9]{4,20}

    Looks like how I would go, but you forgot the dollar on the end to force match start to finish.

    ^[a-zA-Z0-9]{4,20}$

    Rgds,
    Martin.

    For the benefit of all users please mark any post answers as appropriate.
  • Re: Simple Regular Expression (apparently)

    05-03-2007, 9:37 PM
    • Loading...
    • mokeefe
    • Joined on 08-20-2006, 5:15 AM
    • Canberra Australia
    • Posts 2,096

    I'm in error.

    Why would you want to restrict your users from using special characters etc?

    Surely all you want is to enforce a minium requirement, not restrict the password complexity. I agree with the earlier comment about the spaces also.

    Rgds,
    Martin.

    For the benefit of all users please mark any post answers as appropriate.
  • Re: Simple Regular Expression (apparently)

    05-04-2007, 5:04 AM
    • Loading...
    • dotnetuk
    • Joined on 07-28-2004, 11:19 AM
    • Posts 16

    Hi,

    Thanks for your replies, it is much appreciated.

    Bullpit, the example and link is great, thanks for your help,

    John

  • Re: Simple Regular Expression (apparently)

    05-04-2007, 6:31 AM
    • Loading...
    • chandana
    • Joined on 11-17-2006, 1:28 AM
    • Sri Lanka
    • Posts 272
    try with this one .{4,20}
    Chandana
Page 1 of 1 (8 items)
Microsoft Communities
Page view counter