{"id":2764,"date":"2009-12-28T20:30:12","date_gmt":"2009-12-28T19:30:12","guid":{"rendered":"http:\/\/www.chipwreck.de\/blog\/?p=2764"},"modified":"2017-01-11T14:02:39","modified_gmt":"2017-01-11T13:02:39","slug":"mootools-formvalidator-how-to-use-part-2","status":"publish","type":"post","link":"https:\/\/www.chipwreck.de\/blog\/2009\/12\/28\/mootools-formvalidator-how-to-use-part-2\/","title":{"rendered":"Mootools FormValidator &#8211; How to use (Part 2)"},"content":{"rendered":"<div class=\"contentnavi\">\n<p class=\"contentnav1 center\">\n<a href=\"\/blog\/2009\/12\/14\/mootools-formvalidator-how-to-use-part-1\/\">Tutorial Part 1<\/a> &bull;<br \/>\n<span class=\"pink\">Part 2<\/span> &bull;<br \/>\n<a href=\"\/blog\/2010\/01\/12\/mootools-formvalidator-how-to-use-part-3\/\">Part 3<\/a> &bull;<br \/>\n<a href=\"\/blog\/2010\/01\/19\/mootools-formvalidator-how-to-use-part-4\/\">Part 4<\/a> &bull;<br \/>\n<a href=\"\/blog\/2009\/12\/23\/mootools-formvalidator-demo\/\">Live demo<\/a>\n<\/p>\n<\/div>\n<p>This part of the tutorial shows how to trigger the form validation with a link or button and how to build a custom validator.<!--more--><\/p>\n<h2>Submit button<\/h2>\n<p>Our &#8220;register&#8221;-button has no default behaviour, so we add an event which validates and submits the form.<\/p>\n<p>We first check if the button exists &#8211; this is a good practice, since it&#8217;s possible that the same page is loaded without the form (for example if the form was submitted and the page now contains &#8220;Thanks for registering.&#8221; &#8211; but no form anymore).<\/p>\n<p>If the button does exist, we add a click-Event: Namely calling the validate()-method of the FormValidator, which checks all the values and then submits the form:<\/p>\n<pre lang=\"js\">\r\nif ($('registerbtn')) {\r\n\t$('registerbtn').addEvents({\r\n\t\t'click': function() { myFormValidator.validate(); }\r\n\t});\r\n}\r\n<\/pre>\n<div class=\"divider\"><\/div>\n<h2>Custom validators<\/h2>\n<h4>How to create a custom validator<\/h4>\n<p>There are lots of validators present in the mootools-package, but you may soon encounter a situation where none of these is sufficient. Like here:<\/p>\n<p>The username should consist of numbers and letters only. There is a validator called &#8220;validate-alphanum&#8221; already present in the mootools package, but it allows underscores! That&#8217;s not what we want.<\/p>\n<p><small>(This has something to do with regular expressions and might be called a bug, but regardless, we fix it by creating a new validator).<\/small><\/p>\n<p>A validator consists of a <strong>name<\/strong>, an <strong>error-message<\/strong> and a <strong>test-method<\/strong> which does the validation. So we create a custom validator called &#8220;alphaNumFix&#8221;, here our first try:<\/p>\n<p>The error-message simply returns a string, the test method has two arguments: the element being tested and additional properties. To check if the given element only consists of numbers and letters, we use a simple regular expression, into which we feed the element value:<\/p>\n<pre lang=\"js\">\r\nFormValidator.add('alphaNumFix', {\r\n\terrorMsg: 'Username wrong',\r\n\ttest: function(element, props) {\r\n\t\treturn (\/^[a-zA-Z0-9]+$\/).test(element.get('value'));\r\n\t}\r\n});\r\n<\/pre>\n<p>After trying this out we soon realize two problems:<\/p>\n<ul>\n<li>The error message makes sense for our username field, but we can&#8217;t use it for another field.<\/li>\n<li>An empty username would be valid with this test.<\/li>\n<\/ul>\n<p>First the error-message: This validator should simply report what&#8217;s wrong &#8211; not that it&#8217;s wrong for &#8220;username&#8221;. So we should generalize the error-message. But we can do even better: We re-use the error-message of the existing alphaNum-Validator (&#8220;Please use only letters and numbers&#8221;). This gives us the advantage, that there are localized versions available, so we don&#8217;t need to translate this error.<\/p>\n<p>Second try:<\/p>\n<pre lang=\"js\">\r\nFormValidator.add('alphaNumFix', {\r\n\terrorMsg: Form.Validator.getMsg.pass('alphanum'),\r\n\ttest: function(element,props) {\r\n\t\treturn (\/^[a-zA-Z0-9]+$\/).test(element.get('value'));\r\n\t}\r\n});\r\n<\/pre>\n<p>Now about the requirement, that the username can&#8217;t be empty: Here we again re-use an existing validator, so our final &#8220;alphaNumFix&#8221;-Validator looks like this:<\/p>\n<pre lang=\"js\">\r\nFormValidator.add('alphaNumFix', {\r\n\terrorMsg: Form.Validator.getMsg.pass('alphanum'),\r\n\ttest: function(element,props) {\r\n\t\treturn Form.Validator.getValidator('IsEmpty').test(element) || (\/^[a-zA-Z0-9]+$\/).test(element.get('value'));\r\n\t}\r\n});\r\n<\/pre>\n<p>The test function now does this:<\/p>\n<ul>\n<li>Fail, if the element is empty (we validate this by re-using an existing validator)<\/li>\n<li>Do a regex test against the value (only letters or numbers)<\/li>\n<\/ul>\n<h4>Using custom validators<\/h4>\n<p>Assigning this validator to a field is very easy, simply add the validator name to the list of classes:<\/p>\n<pre lang=\"html\">\r\n&lt;input type=\"text\" class=\"alphaNumFix minLength:3 maxLength:30\" name=\"profile[username]\" \/&gt;\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>More about custom validators in <a href=\"\/blog\/2010\/01\/12\/mootools-formvalidator-how-to-use-part-3\/\">part 3<\/a>.<\/p>\n<h4><strong>Comments\/Feedback? Now collected in <a href=\"\/blog\/2010\/01\/19\/mootools-formvalidator-how-to-use-part-4\/\">part 4 &raquo;<\/a><\/strong><\/h4>\n","protected":false},"excerpt":{"rendered":"<p>Tutorial Part 1 &bull; Part 2 &bull; Part 3 &bull; Part 4 &bull; Live demo This part of the tutorial shows how to trigger the form validation with a link or button and how to build a custom validator.<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[14],"tags":[10,54],"class_list":["post-2764","post","type-post","status-publish","format-standard","hentry","category-javascript","tag-mootools","tag-track"],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/paPEN-IA","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.chipwreck.de\/blog\/wp-json\/wp\/v2\/posts\/2764","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.chipwreck.de\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.chipwreck.de\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.chipwreck.de\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.chipwreck.de\/blog\/wp-json\/wp\/v2\/comments?post=2764"}],"version-history":[{"count":1,"href":"https:\/\/www.chipwreck.de\/blog\/wp-json\/wp\/v2\/posts\/2764\/revisions"}],"predecessor-version":[{"id":8047,"href":"https:\/\/www.chipwreck.de\/blog\/wp-json\/wp\/v2\/posts\/2764\/revisions\/8047"}],"wp:attachment":[{"href":"https:\/\/www.chipwreck.de\/blog\/wp-json\/wp\/v2\/media?parent=2764"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.chipwreck.de\/blog\/wp-json\/wp\/v2\/categories?post=2764"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.chipwreck.de\/blog\/wp-json\/wp\/v2\/tags?post=2764"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}