4

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?

1
  • If your comment is relevant to the question you should edit your question and add it. Commented Jul 26, 2013 at 4:41

7 Answers 7

3

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");
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

I've never seen anyone doing it like this, what are the benefits of this? Is it faster?
Yeah, a lookup on an object would be faster than iterating over an array or doing a string search, but the efficiency gain is non-existent at this scale;
I understand, but I like to teach myself to work with the most efficient ways possible. Thanks!
2

Like this

var val = document.getElementById('check_value').value;

if (val  == 'this' || val =='that'  || val =='those'){
     //pass
}

If you want to accept any thing just remove the if condition or check the length of the value

    if (val.length >0){
      //pass
    }

Comments

2

You can use an array for the allowed values and use indexOf to check. Like this:

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

1 Comment

No need. the array is of strings.
1
var correctArr = ['this','that','those'];

function validate(val){
    isCorrect = false
    $.each(correctArr, function(index,element){

            if(val.indexOf(element)>-1){
                isCorrect =  true;
            }
        }
    )
    return isCorrect;
}

Comments

1

You can try regex test like,

var myregex=/this|that|those/i;

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

Updated, if you want to validate only for this,that and those Then try this regex,

var myregex=/^(this|that|those)$/i;

Comments

1

The short answer is no.

You need to compare every single option with the || operator.

An other option would be to use regexp like that :

function validate() {
    var values = ['this', 'that', 'those'];
    if (document.getElementById('check_value').value.search(new RegExp('^('+values.join('|')+')$')) > -1) {
        $('#block').fadeIn('slow');
    }
    else {
        alert("Nope. Try again");
    }
}

Fiddle : http://jsfiddle.net/78PQ8/1/

You can aswell create your own prototype this type of action:

String.prototype.is = function(){
    var arg = Array.prototype.slice.call(arguments);
    return this.search(new RegExp('^('+arg.join('|')+')$')) > -1;
} 

and call is like that:

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

Comments

0

If you want every value to be possible:

var val = document.getElementById('check_value').value;

if( val.length > 0 ) {

}

Which means the string just has to be at least 1 character.. More common way is:

if( val != '' ) {

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.