buydadip
buydadip

Reputation: 9437

Regex to check if password contains at least one capital letter

My regex is not very good, and I've been trying to figure this out by searching online. How would I check if an input value, such as a password, contains at least one capital letter?

So if my password was stored in a variable:

var pass = $("#inputPassword").val();

What would my regex be if I were to use:

regex.test(pass);

Note that is the only condition I am checking for, whether the password contains at least one capital letter, nothing else.

Upvotes: 3

Views: 4355

Answers (3)

Quinn
Quinn

Reputation: 4504

Another regex to try:

var pass = $("#inputPassword").val();
var regex = new RegExp(/^(.*[A-Z].*)$/);
var valid = regex.test(pass)

Upvotes: 1

guessimtoolate
guessimtoolate

Reputation: 8642

/[A-Z]+/ should solve most of your problems. Not sure about unicode support in js, though, but I think you would have to build a regex from ranges of unicode capital characters groups you would like to support (see this helpful table reference). If you were to go that way, you might also take a look at this nifty tool called regenerate.

Upvotes: 1

Saleem
Saleem

Reputation: 8988

I'd be simply checking if it contain any [A-Z] character range.

if (/[A-Z]+/.test($("#inputPassword").val())) {
    // Successful match
} else {
    // Password doesn't contain at least one capital letter
}

Upvotes: 5

Related Questions