Skip to content Skip to sidebar Skip to footer

Regular Expression Testing The New Password

Hello I am working on a javascript that is suppose to test the password field. However I can't seem to get it to work. Nothing bad happens the page loads but seems like the script

Solution 1:

This might be a bit closer to what you want, I've not tested it, but it should be more in the right direction:

functiontest(password)
{
    var patt1 = /[a-z]/g;
    var patt2 = /[A-Z]/g;
    var patt3 = /[0-9]/g;
    var patt4 = /[~\!@#\$%\^&*_\-\+=`\|\\(\)\{\}\[\]:;"'<>,\.\?\/]/g;

    if (!patt1.test(password)) {
        return ("You need a lowercase letter.");
    }

    if (!patt2.test(password)) {
        return ("You need an uppercaseletter.");
    }

    if (!patt3.test(password)) {
        return ("You need a number.");
    }

    if (!patt4.test(password)) {
        return ("You need to use a one of the following characters '~!@#$%^&*_-+=`|\(){}[;.");
    }
}

Solution 2:

This line is at fault:

return ("you need to use a one of the following characters '~!@#$%^&*_-+=`|\(){}[]:;"'<>,.?/'.")

You need to escape the quote and the slash in it.

return ("you need to use a one of the following characters '~!@#$%^&*_-+=`|\\(){}[]:;\"'<>,.?/'.")

Also you use == instead of = for comparisons, true instead of "true". And be in the habbit of always using { } for the contents of your if statements instead of relying on tabbing.

Solution 3:

You need to escape several of those characters in your last regex test:

var patt4 = /[~!@#$%\^&\*_-\+=`\|\(\)\{\}\[\]\:;"'<>,\.\?\/]/g;

If in doubt, escape the character with a backslash \. Nothing will happen if you escape a character that doesn't need escaping.

Post a Comment for "Regular Expression Testing The New Password"