Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to validate multiple values upon form text entry in JavaScript?

Here is my code:

function validate() {
    if (document.getElementById('check_value').value == 'this') {
        $('#block').fadeIn('slow');
    }
    else {
        alert("Nope. Try again");
    }
}

I try doing this:

function validate() {
    if (document.getElementById('check_value').value == 'this','that','those') {
        $('#block').fadeIn('slow');
    }
    else {
        alert("Nope. Try again");
    }
}

...and it just accepts absolutely anything that's entered. Is this even possible to do?

like image 998
RandomPleb Avatar asked Dec 25 '22 23:12

RandomPleb


1 Answers

This works too.

function validate() {
    var acceptableValues = { 
        this: undefined, 
        that: undefined, 
        those: undefined 
    },
    value = document.getElementById('check_value').value;

    if ( acceptableValues.hasOwnProperty(value) ) {
        $('#block').fadeIn('slow');
    }
    else {
        alert("Nope. Try again");
    }
}

and this:

function validate() {
    var value = document.getElementById('check_value').value;

    switch (value) {
        case: 'this':
        case: 'that':
        case: 'those':
            $('#block').fadeIn('slow'); break;
        default:
            alert("Nope. Try again");
    }
}
like image 50
Cory Danielson Avatar answered Dec 29 '22 11:12

Cory Danielson