REGEX password must contain letters a-zA-Z and at least one digit 0-9 RRS feed

  • Question

  • User2606139 posted
    I need help how to make regex to validate password if it contains at least one character [a-zA-Z] and at least one digit [0-9]??

    I look for any digit .*[0-9].* and for any character .*[a-zA-Z]+.* but I don't know how to connect them to search for at least one digit in any position and one character in any position (any appearance of)

    Sunday, September 11, 2005 2:15 PM

All replies

  • User-873196719 posted

    This isn't easily done with 1 regex.

    You need one regex to test for a letter and another to test for a digit.

    If you say the password must start with a letter, then the regex is simple:  @"^[A-Za-z]+\d+.*$"

    Sunday, September 11, 2005 7:09 PM
  • User-831632110 posted
    How about this...

    This will check for text string to have atleast one Numeric and one Character and the length of text string to be
    between 4 to 8 characters.....Try this out...Njoy!

    Sunday, September 11, 2005 8:25 PM
  • User-873196719 posted

    I didn't realize that the RegEx engine did look ahead.
    Sunday, September 11, 2005 9:23 PM
  • User2606139 posted

    That is great However unfortunatly it do not accept strings like "mypass1" because there is no letter after digit. it is accepting only "my1pass" or "1mypass".

    What I need is to do exactly that what do your regex but without important order of appearance

    Moreover (i don't know why) but it do not accept string where is more digits then characters like : "35463pas". But it is not a big problem...

    Is it possible to make your regex that will ignore the order of appearance?

    Monday, September 12, 2005 3:59 AM
  • User2606139 posted
    I FOUND THAT!!! Great thanks for akchamarthy

    It was easy;) I googled your regex and I found some similar another versions of that I have combined them :) and I got exactly that what I need without important order of appearance :

    Actual Regex is : ^.*(?=.{4,10})(?=.*\d)(?=.*[a-zA-Z]).*$

    1) Search for at least one digit in any position
    2) Search for at least one upper or lower case in any position
    3) Enforce password to consist of 4-10 characters

    Now it's exactly what I need;)

    THANKS akchamarthy again:)!! I've just added ".*" twice and changed string leanght controlelr??(can it be called like that?) :)

    Best Regards
    Monday, September 12, 2005 4:09 AM
  • User-831632110 posted
    Cool...Glad i was of some help....Njoy!
    Monday, September 12, 2005 8:57 AM
  • User-916962509 posted

    I know this is a nearly-3-year-old post so sorry to post a reply. 

    Just wanted to say thank you for posting this regular expression. I've spent most of today googling for it, and finally typed just the right thing into Google to find this page :)

    I did modify it just slightly; because your regex would allow special characters and space characters. I wanted to validate a username with the rules 1. must be at least six characters; 2. must contain only letters or numbers; 3. must contain at least one letter.

    To solve my problem, I adapted your regex slightly into:



    Thanks again!

    Thursday, October 2, 2008 11:10 AM
  • User820639650 posted

    A huge THANX to both akchamarthy and Dealie for both your examples were exactly what I was looking for to come up with a quick win on a late friday evening issue - I know very little about RegEx, and needed to craft something out of thin air. Wouldn't be able to do it without these posts. Still good after 5 years!!

    You guys are champs! I owe you a lot!

    Monday, September 20, 2010 6:28 AM
  • User-1312360597 posted

    Old thread, I know - I am looking at a very similar regex, and I'm almost there, but need some help finishing it off. Here's what I need:

    1) 6-10 characters

    2) At least one alpha AND one number

    3) The following special chars are allowed (0 or more): !@#$%

    Here's what I have:


    It works on all my test cases, except it allows "a1234567890", "testIt!0987", and "1abcdefghijk", none of which should be allowed, since they contain more than 10 chars.

    Any ideas?

    Monday, September 27, 2010 12:07 PM
  • User-196574331 posted

    Late, but as the topic is well referenced it deserves an answer in case someone needs the same


    1) 6-10 characters

    2) At least one alpha AND one number

    3) The following special chars are allowed (0 or more): !@#$%

    based on akchamarthy's regexp


    to explain it, consider it as 3 individual parts

    (?=.*\d+)  ===> the chain must contain a digit


    (?=.*[a-zA-Z])  ===> the chain must contain an alpha


    [0-9a-zA-Z!@#$%]{6,10}  ===> the chain can only contain digit, alpha and the special characters "!@#$%" with a size limit of {6,10}

    Glad I found this topic BTW, because I didn't know either that something like lookahead ('?=' ) existed.

    Wednesday, July 6, 2011 6:15 AM
  • User1518303729 posted


    i need a code for mail id shoud contain atleast 6 chareters and user id should not contain number values, and no empty spaces. and password length shud be 6 to 8 charecters and password contain one special charecter and atleast one digit and atleast one capital letter. please give me reply with answer

    Wednesday, April 2, 2014 8:41 AM
  • User1207564159 posted

    Simple solution to check for password containing at least one letter, at least one digit and no spaces:

    Thursday, January 29, 2015 7:48 AM
  • User1988957019 posted

    Hi All,We have a requirement asking for atleast 3 out of 4 variables(for Password). Variables are:

    1. small Letters[a-z]

    2. Capital letters[A-Z]

    3. digit[0-9]

    4. Special Characters(Except Space)

    5. Atleast 7 characters

    I tried the below expression(tried with 3 of the above variables):

    ^(.*[0-9]+.*[a-z]+.*[A-Z]+.*)|(.*[0-9]+.*[A-Z]+.*[a-z]+.*)|(.*[a-z]+.*[0-9]+.*[A-Z]+.*)|(.*[a-z]+.*[A-Z]+.*[0-9]+.*)|(.*[A-Z]+.*[a-z]+.*[0-9]+.*)|(.*[A-Z]+.*[0-9]+.*[a-z]+.*)$ and this is working but as soon as I add the condition of minimum of 7 characters it throws an "Missing operand to apache.org.regexp" for below expression.

    Request you all to please guide as what I am doing wrong.


    Tuesday, March 15, 2016 4:53 AM