Osman
Osman

Reputation: 1771

Verify password contains letter

$('#password').val().indexOf(/[a-z]/) != -1) 

Does this work? if so is there something wrong with it because its not working.

Upvotes: 0

Views: 321

Answers (3)

Talha Ahmed Khan
Talha Ahmed Khan

Reputation: 15433

No this will not work because you have to use match() function for that.

I also recommend that if you want only numbers then you should try to validate only numbers.

Do not allow anything else except number.

if( $('#password').val().match(/[0-9]+/) ){
    // Contains only numbers from 0-9
}

Upvotes: 0

Augustus Kling
Augustus Kling

Reputation: 3333

Do your check like the following because you want to work with an object supporting regular expression syntax:

var valContainsLetter = /[a-z]/i.test($('#password').val())

The i flag is to support lower and upper case letters by the way.

Upvotes: 1

Quentin
Quentin

Reputation: 943591

The indexOf method accepts a string, not a regular expression.

If you want to test a regular expression then use match

Upvotes: 5

Related Questions