0

I want to do validation of a password field on a login form. The requirement for the password is that the password should be combination of alphabet and numbers. I write a new validation function to fulfil above requirement and added it to jQuery using validator.addmethod(). The function code is as follows

$.validator.addMethod('alphanum', function (value) {
    return /^[a-zA-Z 0-9]+$/.test(value);
}, 'Password should be Alphanumeric.');

Problem is this function is not working properly i.e. it accepts alphabetic password (like abcdeSD) and numerical password (like 4255345) and dot give error message for such inputs.

  1. so is there anything wrong in my code?
  2. is the written regular expression is wrong and if yes then what will be the correct reg expression?
4
  • Accept some answers! You've got a 0% rate. Commented Feb 15, 2010 at 9:58
  • Hi Gumbo! My aim to validate password is to avoid sql injection because of that I am validating password. Commented Feb 15, 2010 at 10:06
  • 1
    If SQL injection is your only reason for validating then this is not the right way to do it. It looks like you're trying to encourage people to increase their password strength (but you allow passwords of length 1?). Commented Feb 15, 2010 at 10:10
  • Thanks Mark! for your comment but I am not allowing passwords of length 1. I have made the validation for password length using maxlength and minlength. Commented Feb 15, 2010 at 11:05

3 Answers 3

2

Use negative lookaheads to disallow what you don't want:

^(?![a-zA-Z]+$)(?![0-9]+$)[a-zA-Z 0-9]+$
Sign up to request clarification or add additional context in comments.

1 Comment

Thank You Mark! The regular expression you have given is working perfect man. thank you very much
0

Password must contain only alphabets and digits

/^[a-zA-Z0-9]+$/

It must contain alphabet(s)

/[a-zA-Z]+/

It must contain digit(s)

/[0-9]+/

And'ing these

/^[a-zA-Z0-9]+$/.test(value) && /[a-zA-Z]+/.test(value) && /[0-9]+/.test(value)

Comments

0

Use this expression for password for javascript:

/((?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%_!]).{8,8})/

You can add the any special characters in the expression.

Manually add the characters for in [@#$%_!] inside.

Comments

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.