Grow your CSS skills. Land your dream job.

Check if input value is >= 8 & <=32 & has at least 1 number!?

  • # May 20, 2013 at 7:50 pm

    Pretty sure I’m not doing this correctly:

    if (isNaN( $(“#field”).val() ) && val.length >= min && val.length < = max) {
    showMessage(‘Your password is good!’);
    }

    http://jsfiddle.net/wGVMW/

    # May 20, 2013 at 8:08 pm

    it works?

    But i would not use javascript to do this because its easy for the user to alter.

    # May 20, 2013 at 8:11 pm

    Since you already have `val` defined, you can just do this:

    if (isNaN(val) && val.length >= min && val.length < = max) {
    showMessage(‘Your password is good!’);
    }

    Otherwise it looks good.

    # May 20, 2013 at 8:22 pm

    Still isn’t working correctly. :/ I want it to say “Your password is good!” ONLY when there is at least one number in the password, and the password itself needs to be >= 8 chars, and <= 32 chars. I’m sorry if I didn’t seem clear. Thanks for the help Mottie!

    # May 20, 2013 at 8:41 pm

    Try this ([demo](http://jsfiddle.net/Mottie/wGVMW/1/)):

    $(document).ready(function () {

    var $field = $(‘#field’),
    nul = 0,
    min = 8,
    max = 32,
    $h4 = $(‘h4′),
    showMessage = function (msg) {
    $h4.hide().stop(true, true).text(msg).fadeIn(200).delay(1200).fadeOut(200);
    };

    $(‘#btn’).click(function () {
    var val = $field.val();

    if (val.length < min) {
    showMessage(‘Password is too short!’);
    } else if (val.length > max) {
    showMessage(‘Password is too long!’);
    } else if (val.length == nul) {
    showMessage(‘Enter in a password!’);
    } else if (!/d/.test(val)) {
    showMessage(“Password needs at least one number!”);
    } else if (/d/.test(val) && val.length >= min && val.length < = max) {
    showMessage(‘Your password is good!’);
    }

    });
    });

    # May 20, 2013 at 8:50 pm

    Thanks for the help man! It works great.

    # May 21, 2013 at 7:35 am

    } else if (val.length == nul) {
    showMessage(‘Enter in a password!’);

    will never get reached (nothing filled in shows ‘Password is too short!’)

    } else if (/d/.test(val) && val.length >= min && val.length < = max) {

    don’t need the if, just `} else {`, because you already know everything else evaluates to true at that point ;)

Viewing 8 posts - 1 through 8 (of 8 total)

You must be logged in to reply to this topic.

*May or may not contain any actual "CSS" or "Tricks".