{"id":428,"date":"2022-08-30T15:01:11","date_gmt":"2022-08-30T15:01:11","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/09\/is-it-possible-to-validate-multiple-values-upon-form-text-entry-in-javascript-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:01:11","modified_gmt":"2022-08-30T15:01:11","slug":"is-it-possible-to-validate-multiple-values-upon-form-text-entry-in-javascript-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/is-it-possible-to-validate-multiple-values-upon-form-text-entry-in-javascript-collection-of-common-programming-errors\/","title":{"rendered":"Is it possible to validate multiple values upon form text entry in JavaScript?-Collection of common programming errors"},"content":{"rendered":"<p>The short answer is no.<\/p>\n<p>You need to compare every single option with the <code>||<\/code> operator.<\/p>\n<p>An other option would be to use regexp like that :<\/p>\n<pre><code>function validate() {\n    var values = ['this', 'that', 'those'];\n    if (document.getElementById('check_value').value.search(new RegExp('^('+values.join('|')+')$')) &gt; -1) {\n        $('#block').fadeIn('slow');\n    }\n    else {\n        alert(\"Nope. Try again\");\n    }\n}\n<\/code><\/pre>\n<p>Fiddle : http:\/\/jsfiddle.net\/78PQ8\/1\/<\/p>\n<p>You can aswell create your own prototype this type of action:<\/p>\n<pre><code>String.prototype.is = function(){\n    var arg = Array.prototype.slice.call(arguments);\n    return this.search(new RegExp('^('+arg.join('|')+')$')) &gt; -1;\n} \n<\/code><\/pre>\n<p>and call is like that:<\/p>\n<pre><code>function validate() {\n    if (document.getElementById('check_value').value.is('this', 'that', 'those')) {\n        $('#block').fadeIn('slow');\n    }\n    else {\n        alert(\"Nope. Try again\");\n    }\n}\n<\/code><\/pre>\n<p id=\"rop\"><small>Originally posted 2013-11-09 19:41:38. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>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 = [&#8216;this&#8217;, &#8216;that&#8217;, &#8216;those&#8217;]; if (document.getElementById(&#8216;check_value&#8217;).value.search(new RegExp(&#8216;^(&#8216;+values.join(&#8216;|&#8217;)+&#8217;)$&#8217;)) &gt; -1) { $(&#8216;#block&#8217;).fadeIn(&#8216;slow&#8217;); } else { alert(&#8220;Nope. Try again&#8221;); } } Fiddle : http:\/\/jsfiddle.net\/78PQ8\/1\/ You [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-428","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/428","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=428"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/428\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=428"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=428"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=428"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}