简体   繁体   中英

Alphanumeric validation

I can't understand why this alphanumeric check/validation doesn't work. I've cut the other switch options (that all work) 'cause the only check which doesn't work is the else if (!(pswRegex.test(inputValue))) . I'm not a jQuery/Javascript expert, so am I missing something?

The regex check should allow a password only if it's composed by letters (capital or non capital) and numbers.

$(document).ready(function(){

    $('.req').on('input', function() {
        var inputID = $(this).attr('id');
        var inputValue = $(this).val();
        var valueLenght = $(this).val().length;

        switch (inputID) {
            case "psw":
            var pswRegex = /^[a-zA-Z0-9]+$/;
            if (valueLenght < 8) {
                $('#subButton').prop('disabled', true);
                $('#err' + inputID).show().text("Password must be 8 carachters minimum");
            }
            else if (!(pswRegex.test(inputValue))) { 
                $('#subButton').prop('disabled', true);
                $('#err' + inputID).show().text("Password must be alphanumeric");
            }
            else {
                $('#subButton').prop('disabled', false);
                $('#err' + inputID).hide();
            }
        break;
        }
  });

});

[EDIT] : what I wanted to achieve was displaying an error if the inputValue string contained only letters or only numbers: the user has to provide an alphanumeric password. Thanks to Aioros and Peeyush Kushwaha I understood that I can't achieve this with only a regex (at least with my actual knowledge about regular expressions), so I changed my else if condition with this one:

else if (/^[0-9]+$/.test(inputValue) || /^[a-zA-Z]+$/.test(inputValue)) { 
                $('#subButton').prop('disabled', true);
                $('#err' + inputID).show().text("Password must be composed by both letters and numbers");
            }

[EDIT 2] : another clean and elegant solution is the one provided by PanterA . With only one if condition you can display (only) one error statement to the user saying that the password must be at least 8 carachters long and composed by, at least: one capital letter, one lower case letter and one number.

var pswRegex = /^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9]).{8,}$/;
if (!pswRegex.test(inputValue)) {
    $('#subButton').prop('disabled', true);
    $('#err' + inputID).show().text("Password must be at least 8 carachters long and composed by, at least, one capital letter, one lower case letter and one number");
}

This regex may help you:

/^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9]).*$/

The positive lookahead ( ?= ) matches anything inside its parenthesis without being part of the result. Here we want the regex to find the minimal occurence of anything ( .*? ) followed by the caracters we want to enforce (Uppercase letters for example).

So this regex will check:

  • At least one upper case letter: (?=.*?[AZ])
  • At least one lower case letter: (?=.*?[az])
  • At least one digit: (?=.*?[0-9])

Check it out: https://jsfiddle.net/q7do1woL/

Also, If you want to validate the length of the string with regex, you can add a quantifier with a minimal value:

/^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9]).{8,}$/

Hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM