1

I have a problem with my password validation using regex on javascript. the criteria are :

  1. have at least one or more letter(it can be upper case or lower case)
  2. have at least one or more number
  3. character length minimal 3 and maximal 30

I hope anyone can help me to solve this problem :)

var ch_pass = /^([0-9]+).([a-zA-Z]+).({3,30})$/;

1
  • You can easily check #3 by checking the value's .length. For #1 and #2, just use two regular expressions to check that the value contains a alpha and numeric character each. For example: var value = "YOUR INPUT", alpha = /[A-Za-z]/, numeric = /[0-9]/; if (value.length < 3 || value.length > 30 || !alpha.test(value) || !numeric.test(value)) { /* INVALID */ }. It's a lot easier to read/understand/maintain, in my opinion Commented Mar 5, 2014 at 19:36

2 Answers 2

2

You can use lookahead like this:

var ch_pass = /^(?=.*?[0-9])(?=.*?[a-zA-Z]).{3,30}$/;
Sign up to request clarification or add additional context in comments.

2 Comments

Glad to know, can you mark the answer as accepted by clicking on tick mark on top-left of my answer.
That is lazy vs non-lazy matching
2

I wouldn't recommend trying to do this whole check in a single regular expression because it just over complicates it. Do each condition individually.

  1. Has at least one letter:

    var has_letters = (/[a-zA-Z]/).test(password);
    
  2. Has at least one number:

    var has_numbers = (/[0-9]/).test(password);
    
  3. Has between 3 and 30 characters (inclusive):

    var has_length = 3 <= password.length && password.length <= 30;
    

This can all be wrapped up into a function:

function is_password_valid(password) {
    var has_letters = (/[a-zA-Z]/).test(password);
    var has_numbers = (/[0-9]/).test(password);
    var has_length = 3 <= password.length && password.length <= 30;
    return has_letters && has_numbers && has_length;
}

Or if you prefer something more dense:

function is_password_valid(password) {
    return ((/[a-zA-Z]/).test(password)
        && (/[0-9]/).test(password)
        && password.length >= 3
        && password.length <= 30);
}

6 Comments

Not that it matters, but regex.test makes more sense to use, instead of .match() != null. And for #3, you left out the "maximum 30" part
@Ian Thank you, I wasn't aware of .test().
Sorry, test is a method of a regular expression object. You'd use var alpha = /[a-zA-Z]/; and then alpha.test(password) - it returns a boolean if it matches or not
@Ian I guess that's what I get for not double checking.
As a reference to you or anyone else: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… . And I'd read that page, especially the Description because of the quirk when using a global search. Your regexes don't do that, so it should be fine, but in case of future use
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.