Skip to content Skip to sidebar Skip to footer

How Do I Validate Against Two Possible Values?

How do I get the function below to validate against two values. e.g. Match the form input against 'man' and 'stevesho.com' not just 'man'. function access() { if(document.getE

Solution 1:

function access()
{  
    var letters = document.getElementById('letters').value;  
    var goto_link = "";
    switch(letters){  
        case'man': 
        case'stevesho.com':
            goto_link = 'http://www.google.com';
            break;
        case'woman':
            goto_link = 'http://www.dynamicdrive.com';
            break;
        case'cat':
            goto_link = 'http://www.youtube.com';
            break;
        case'dog':
            goto_link = 'http://www.dailymotion.com';
            break;
        default: 
            alert('Access denied. Try again.')
            return;
    }
    location.href = goto_link;
}

Solution 2:

Are you looking for the OR operator? See http://www.w3schools.com/js/js_comparisons.asp

var letter = document.getElementById('letters').valueif (letter == 'man' || letter == 'stevesho.com')
    location.href = 'http://www.google.com'

Another option that might be of interest;

var key = document.getElementById('letters').valuevar collection = {
    "key1" : "url1",
    "key2" : "url2",
    "key3" : "url3",
    "man" : "http://www.google.com",
    "stevesho.com" : "http://www.google.com"
};

if (collection[key]) {
    var url = collection[key];
    alert(key + " = " + url); // DEBUG//location.href = url;
}
else {
    alert("Access denied. Try again.");
}

Solution 3:

It can be:

functionaccess()
{
    var s = document.getElementById('letters').value;
    if(s =='man' || s = 'stevesho.com')
        location.href='http://www.google.com'elseif(s =='woman')
        location.href='http://www.dynamicdrive.com'elseif(s =='cat')
        location.href='http://www.youtube.com'elseif(s =='dog')
        location.href='http://www.dailymotion.com'elsealert('Access denied. Try again.')
}

Post a Comment for "How Do I Validate Against Two Possible Values?"