Click here to Skip to main content
15,897,291 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am using javascript to validate password but it's fails the requirement. I need Password with More than 5 Character at least one number, one character


What I have tried:

var pwd = /(?=.*\d)(?=.*[a-z]).{6,}/;
var pwd = /^(?=.*[0-9])(?=.*[a-zA-Z])([a-zA-Z0-9]+){6,}$/

both fail on test "King382413"
Posted
Updated 12-Apr-16 0:30am
v2

Don't try to use a single Regex - it's not at all simple.
Instead, do it in javascript as a number of checks: How to Build a Password Validator with JavaScript – Matt Litzinger[^]
That way, when your password requirements change (as they should, you should allow - if not insist on - special characters as well) it's a heck of a lot simpler to change them!
 
Share this answer
 
Comments
itsathere 12-Apr-16 3:25am    
Yes, it fine but what about if i use regular expression like below but getting error ""#" is not valid at the start of a code block. Only identifiers, keywords, comments, "(" and "{" are valid." due to razor

var pwd = /^(?=.*[a-zA-Z])(?=.*\d)(?=.*[!@#$%^&*()_+])[A-Za-z\d][A-Za-z\d!@#$%^&*()_+]{6,}$/
Dave Kreskowiak 12-Apr-16 9:19am    
You're demonstrating why OriginalGriff is saying what he said. If your password requirements change you have to change the RegEx expression. Look at the problems you're having making this work for your initial requirement! You can't do it. So when the password requirements change, you're going to be back at the same problem struggling to figure out a RegEx expression to cover the new requirements.

SIMPLIFY IT. Use separate tests. It's much easier to maintain and, therefor, less expensive to you.
Dave Kreskowiak 12-Apr-16 9:21am    
We can't see your Razor code so it's impossible to tell you what you're doing wrong.

But, what I will say is if you've got javascript, it would be better to put that code in separate .js files and Include those scripts in the Razor code instead of trying to directly embed it. This makes your javascript code reuable for multiple views instead of just the one view page.
itsathere 12-Apr-16 11:23am    
I have got the solution by putting @@ at @.Thanks for your reply.
previous version

var pwd = /^(?=.*[a-zA-Z])(?=.*\d)(?=.*[!@#$%^&*()_+])[A-Za-z\d][A-Za-z\d!@#$%^&*()_+]{6,}$/

new version

var pwd = /^(?=.*[a-zA-Z])(?=.*\d)(?=.*[!@@#$%^&*()_+])[A-Za-z\d][A-Za-z\d!@@#$%^&*()_+]{6,}$/

Try this,

^(?=.*?[0-9])(?=.*[!@#$%])[0-9a-zA-Z!@#$%0-9]{6,}$ 
 
Share this answer
 
Comments
itsathere 12-Apr-16 5:14am    
@ not support in mvc view due to razor engine. I have checked regex
var pwd = /^(?=.*[a-zA-Z])(?=.*\d)(?=.*[!@#$%^&*()_+])[A-Za-z\d][A-Za-z\d!@#$%^&*()_+]{6,}$/
if you are able to modify, reply.
JavaScript
 var pwd = /^(?=.*[a-zA-Z])(?=.*\d)(?=.*[!@@#$%^&*()_+])[A-Za-z\d][A-Za-z\d!@@#$%^&*()_+]{6,}$/

if (pwd.test(Passwordobj.value) == false) {
            errorPasswordobj.innerHTML = "More than 5 Character at least one number, one character.";
return false;
        }


Got idea from here
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900