最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

jquery - Is it possible to validate multiple values upon form text entry in JavaScript? - Stack Overflow

matteradmin3PV0评论

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?

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?

Share Improve this question edited Jul 26, 2013 at 5:22 RandomPleb asked Jul 26, 2013 at 4:23 RandomPlebRandomPleb 4241 gold badge5 silver badges20 bronze badges 1
  • If your ment is relevant to the question you should edit your question and add it. – Keith Smiley Commented Jul 26, 2013 at 4:41
Add a ment  | 

7 Answers 7

Reset to default 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");
    }
}

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
    }

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");
    }
}
var correctArr = ['this','that','those'];

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

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

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;

The short answer is no.

You need to pare 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/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");
    }
}

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 mon way is:

if( val != '' ) {

}
Post a comment

comment list (0)

  1. No comments so far