Is it possible to check multiple values ​​after entering form text in JavaScript?

Here is my code:

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

I am trying to do this:

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

... and he just accepts absolutely everything that came in. Is it possible to do this?

+4
source share
7 answers

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 value

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

You can use an array for allowed values ​​and use indexOf for checking. Like this:

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

This also works.

 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"); } } 
+2
source

The short answer is no.

You need to compare each individual option with the || .

Another option is to use regexp as follows:

 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 also create your own prototype of this type of action:

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

and the call is as follows:

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

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 check only on this,that and those then try regex ,

 var myregex=/^(this|that|those)$/i; 
+1
source
 var correctArr = ['this','that','those']; function validate(val){ isCorrect = false $.each(correctArr, function(index,element){ if(val.indexOf(element)>-1){ isCorrect = true; } } ) return isCorrect; } 
+1
source

If you want every value to be possible:

 var val = document.getElementById('check_value').value; if( val.length > 0 ) { } 

This means that the string must be at least 1 character. More common way:

 if( val != '' ) { } 
0
source

Source: https://habr.com/ru/post/1493574/


All Articles