{"id":1352,"date":"2022-08-30T15:15:49","date_gmt":"2022-08-30T15:15:49","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/13\/validator-remote-method-matching-value-to-json-array-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:15:49","modified_gmt":"2022-08-30T15:15:49","slug":"validator-remote-method-matching-value-to-json-array-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/validator-remote-method-matching-value-to-json-array-collection-of-common-programming-errors\/","title":{"rendered":"Validator remote method, matching value to JSON array-Collection of common programming errors"},"content":{"rendered":"<p>Aloha!<\/p>\n<p>Trying to run a basic validation on a postal code input, and additionally check the entered value to see if it exists in a keyless JSON array. Everything works as expected until I add the remote function. If possible, I&#8217;d like to utilize this function, since it is built in, but I am open to other methods of validation.<\/p>\n<p>Working example of this, without remote, here: http:\/\/jsfiddle.net\/sangria\/xLGae\/<\/p>\n<p>Likely unrelated, but getting this error in the console on submit:<\/p>\n<pre><code>Uncaught Error: NOT_FOUND_ERR: DOM Exception 8\n<\/code><\/pre>\n<p>JSON array format:<\/p>\n<pre><code>[94102,94117,94110,99950,...]\n<\/code><\/pre>\n<p>jQuery and Validation:<\/p>\n<pre><code>jQuery.validator.addMethod(\"postalcode\", function(postalcode, element) {\n    return this.optional(element) || postalcode.match(\/^([0-9]{5})$\/);\n}, \"Please specify a valid postal\/zip code\");\n\n$(\"#postal\").validate({\n    rules: {\n        zipcode: {\n            required: true,\n            postalcode: true,\n            digits: true,\n            minlength: 5,\n            maxlength: 5,\n            remote: {\n                type: 'post',\n                contentType: 'application\/json; charset=utf-8',\n                url: \"data\/uszipsonly.json\",\n                dataType: 'json',\n                async: false\n            }\n        }\n    },\n    messages: {\n        zipcode: { remote: \"Please specify a US postal code\"}\n    }\n});\n<\/code><\/pre>\n<p>Pored over the docs and some other good examples. Found similar threads here on SO, but still somewhat tripped up by this.<\/p>\n<p>Much thanks to the community for any enlightenment.<\/p>\n<ol>\n<li>\n<p>If you are using the jquery.bassistance.de like I think maybe you mis-interpreted the role of the remote feature..<\/p>\n<p>Basically remote can only return true\/false after being sent the value and is intended mainly for rules were you don&#8217;t want the accepted value to be stored on the client, EVER (think of captcha for example) like in here http:\/\/jquery.bassistance.de\/validate\/demo\/captcha\/<\/p>\n<p>If you want to download the list of possible ZIP code your app accept (or does not accept) from your server I suggest doing the following:<\/p>\n<pre><code>$.getJSON(\"data\/uszipsonly.json\", function(response) {\nvar validZIP = response;\n\njQuery.validator.addMethod(\"postalcode\", function(postalcode, element) {\n    return this.optional(element) || (postalcode.match(\/^([0-9]{5})$\/) &amp;&amp; $.inArray(postalcode, validZIP));\n}, \"Please specify a valid postal\/zip code\");\n<\/code><\/pre>\n<p>})<\/p>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-13 09:49:02. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>Aloha! Trying to run a basic validation on a postal code input, and additionally check the entered value to see if it exists in a keyless JSON array. Everything works as expected until I add the remote function. If possible, I&#8217;d like to utilize this function, since it is built in, but I am open [&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-1352","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1352","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=1352"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1352\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1352"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1352"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1352"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}