{"id":445,"date":"2015-04-19T07:35:35","date_gmt":"2015-04-19T06:35:35","guid":{"rendered":"http:\/\/dknzdesign.com\/?p=445"},"modified":"2015-04-19T07:52:05","modified_gmt":"2015-04-19T06:52:05","slug":"how-to-validate-form-in-magento","status":"publish","type":"post","link":"https:\/\/dknzdesign.com\/sl\/blog\/magento\/how-to-validate-form-in-magento\/","title":{"rendered":"Kako potrditi obrazec v magentu"},"content":{"rendered":"<p>As you know, Magento uses Prototype as javascript library.\u00a0 It provides a simple way to validate html form values.<\/p>\n<p>Below is an example:<\/p>\n<pre>&lt;form name=\"my-form\" id=\"my-form\" method=\"post\"&gt;\r\n&lt;label for=\"firstname\"&gt;\r\n&lt; ?php echo $this-&gt;__('First name') ?&gt; &lt;span&gt;*&lt;\/span&gt;&lt;\/label&gt;&lt;br \/&gt;\r\n&lt;input\u00a0 id=\"firstname\" name=\"firstname\" class=\"input-text  required-entry\"\/&gt;\r\n\r\n&lt;label  for=\"lastname\"&gt;\r\n&lt; ?php echo $this-&gt;__('Last name') ?&gt;  &lt;span&gt;*&lt;\/span&gt;&lt;\/label&gt;&lt;br \/&gt;\r\n&lt;input\u00a0 id=\"lastname\" name=\"lastname\" class=\"input-text   required-entry\"\/&gt;\r\n\r\n&lt;\/form&gt;\r\n\r\n&lt;script type=\"text\/javascript\"&gt;\r\n\/\/&lt; ![CDATA[\r\nvar customForm = new VarienForm('my-form');\r\n\/\/]]>\r\n&lt;\/script&gt;<\/pre>\n<p>In the form above you can see each input field has its own class name and these classes will be used to validate field\u2019s value. There are many predefined classes like that in prototype lib. I\u2019ll show you all of them at the end of this topic.<\/p>\n<p>Most important thing besides assigning class names is that little piece of JavaScript below the form. Remember to pass form id into the <em><strong>new VarienForm<\/strong><\/em> object.<\/p>\n<p>Below is the list of validate class and its error message that definded in prototype lib.<\/p>\n<table class=\"blog-table\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<thead>\n<tr>\n<td class=\"first\"><strong>Validate Class<\/strong><\/td>\n<td class=\"last\"><strong>Meaning<\/strong><\/td>\n<\/tr>\n<\/thead>\n<tbody>\n<tr class=\"odd\">\n<td class=\"first\">validate-select<\/td>\n<td>Please select an option.<\/td>\n<\/tr>\n<tr class=\"even\">\n<td class=\"first\">required-entry<\/td>\n<td>This is a required field.<\/td>\n<\/tr>\n<tr class=\"odd\">\n<td class=\"first\">validate-number<\/td>\n<td>Please enter a valid number in this field.<\/td>\n<\/tr>\n<tr class=\"even\">\n<td class=\"first\">validate-digits<\/td>\n<td>Please use numbers only in this field. please avoid spaces or other characters such as dots or commas.<\/td>\n<\/tr>\n<tr class=\"odd\">\n<td class=\"first\">validate-alpha<\/td>\n<td>Please use letters only (a-z or A-Z) in this field.<\/td>\n<\/tr>\n<tr class=\"even\">\n<td class=\"first\">validate-code<\/td>\n<td>Please use only letters (a-z), numbers (0-9) or underscore(_) in this field, first character should be a letter.<\/td>\n<\/tr>\n<tr class=\"odd\">\n<td class=\"first\">validate-alphanum<\/td>\n<td>Please use only letters (a-z or A-Z) or numbers (0-9) only in this field. No spaces or other characters are allowed.<\/td>\n<\/tr>\n<tr class=\"even\">\n<td class=\"first\">validate-street<\/td>\n<td>Please use only letters (a-z or A-Z) or numbers (0-9) or spaces and # only in this field.<\/td>\n<\/tr>\n<tr class=\"odd\">\n<td class=\"first\">validate-phoneStrict<\/td>\n<td>Please enter a valid phone number. For example (123) 456-7890 or 123-456-7890.<\/td>\n<\/tr>\n<tr class=\"even\">\n<td class=\"first\">validate-phoneLax<\/td>\n<td>Please enter a valid phone number. For example (123) 456-7890 or 123-456-7890.<\/td>\n<\/tr>\n<tr class=\"odd\">\n<td class=\"first\">validate-fax<\/td>\n<td>Please enter a valid fax number. For example (123) 456-7890 or 123-456-7890.<\/td>\n<\/tr>\n<tr class=\"even\">\n<td class=\"first\">validate-date<\/td>\n<td>Please enter a valid date.<\/td>\n<\/tr>\n<tr class=\"odd\">\n<td class=\"first\">validate-email<\/td>\n<td>Please enter a valid email address. For example johndoe@domain.com.<\/td>\n<\/tr>\n<tr class=\"even\">\n<td class=\"first\">validate-emailSender<\/td>\n<td>Please use only letters (a-z or A-Z), numbers (0-9) , underscore(_) or spaces in this field.<\/td>\n<\/tr>\n<tr class=\"odd\">\n<td class=\"first\">validate-password<\/td>\n<td>Please enter 6 or more characters. Leading or trailing spaces will be ignored.<\/td>\n<\/tr>\n<tr class=\"even\">\n<td class=\"first\">validate-admin-password<\/td>\n<td>Please enter 7 or more characters. Password should contain both numeric and alphabetic characters.<\/td>\n<\/tr>\n<tr class=\"odd\">\n<td class=\"first\">validate-cpassword<\/td>\n<td>Please make sure your passwords match.<\/td>\n<\/tr>\n<tr class=\"even\">\n<td class=\"first\">validate-url<\/td>\n<td>Please enter a valid URL. http:\/\/ is required<\/td>\n<\/tr>\n<tr class=\"odd\">\n<td class=\"first\">validate-clean-url<\/td>\n<td>Please enter a valid URL. For example http:\/\/www.example.com or www.example.com<\/td>\n<\/tr>\n<tr class=\"even\">\n<td class=\"first\">validate-identifier<\/td>\n<td>Please enter a valid Identifier. For example example-page, example-page.html or anotherlevel\/example-page<\/td>\n<\/tr>\n<tr class=\"odd\">\n<td class=\"first\">validate-xml-identifier<\/td>\n<td>Please enter a valid XML-identifier. For example something_1, block5, id-4<\/td>\n<\/tr>\n<tr class=\"even\">\n<td class=\"first\">validate-ssn<\/td>\n<td>Please enter a valid social security number. For example 123-45-6789.<\/td>\n<\/tr>\n<tr class=\"odd\">\n<td class=\"first\">validate-zip<\/td>\n<td>Please enter a valid zip code. For example 90602 or 90602-1234.<\/td>\n<\/tr>\n<tr class=\"even\">\n<td class=\"first\">validate-date-au<\/td>\n<td>Please use this date format: dd\/mm\/yyyy. For example 17\/03\/2006 for the 17th of March, 2006.<\/td>\n<\/tr>\n<tr class=\"odd\">\n<td class=\"first\">validate-currency-dollar<\/td>\n<td>Please enter a valid $ amount. For example $100.00.<\/td>\n<\/tr>\n<tr class=\"even\">\n<td class=\"first\">validate-one-required<\/td>\n<td>Please select one of the above options.<\/td>\n<\/tr>\n<tr class=\"odd\">\n<td class=\"first\">validate-one-required-by-name<\/td>\n<td>Please select one of the options.<\/td>\n<\/tr>\n<tr class=\"even\">\n<td class=\"first\">validate-not-negative-number<\/td>\n<td>Please enter a valid number in this field.<\/td>\n<\/tr>\n<tr class=\"odd\">\n<td class=\"first\">validate-state<\/td>\n<td>Please select State\/Province.<\/td>\n<\/tr>\n<tr class=\"even\">\n<td class=\"first\">validate-new-password<\/td>\n<td>Please enter 6 or more characters. Leading or trailing spaces will be ignored.<\/td>\n<\/tr>\n<tr class=\"odd\">\n<td class=\"first\">validate-greater-than-zero<\/td>\n<td>Please enter a number greater than 0 in this field.<\/td>\n<\/tr>\n<tr class=\"even\">\n<td class=\"first\">validate-zero-or-greater<\/td>\n<td>Please enter a number 0 or greater in this field.<\/td>\n<\/tr>\n<tr class=\"odd\">\n<td class=\"first\">validate-cc-number<\/td>\n<td>Please enter a valid credit card number.<\/td>\n<\/tr>\n<tr class=\"even\">\n<td class=\"first\">validate-cc-type<\/td>\n<td>Credit card number doesn\\\u2019t match credit card type<\/td>\n<\/tr>\n<tr class=\"odd\">\n<td class=\"first\">validate-cc-type-select<\/td>\n<td>Card type doesn\\\u2019t match credit card number<\/td>\n<\/tr>\n<tr class=\"even\">\n<td class=\"first\">validate-cc-exp<\/td>\n<td>Incorrect credit card expiration date<\/td>\n<\/tr>\n<tr class=\"odd\">\n<td class=\"first\">validate-cc-cvn<\/td>\n<td>Please enter a valid credit card verification number.<\/td>\n<\/tr>\n<tr class=\"even\">\n<td class=\"first\">validate-data<\/td>\n<td>Please use only letters (a-z or A-Z), numbers (0-9) or underscore(_) in this field, first character should be a letter.<\/td>\n<\/tr>\n<tr class=\"odd\">\n<td class=\"first\">validate-css-length<\/td>\n<td>Please input a valid CSS-length. For example 100px or 77pt or 20em or .5ex or 50%<\/td>\n<\/tr>\n<tr class=\"even\">\n<td class=\"first\">validate-length<\/td>\n<td>Maximum length exceeded.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Hope this Magento tutorial helps you to validate your custom form.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Kot veste, Magento uporablja Prototype kot knji\u017enico javascript. Ponuja preprost na\u010din za preverjanje vrednosti obrazca html. Spodaj je primer: &lt;form name=&quot;&rdquo;my-form&rdquo;&quot; id=&quot;&rdquo;my-form&rdquo;&quot; method=&quot;&rdquo;post&rdquo;&quot; action=&quot;&quot;&gt; &lt;label for=&quot;&rdquo;firstname&rdquo;&quot;&gt; &amp;lt; ?php echo $this-&amp;gt;__(&amp;#039;Ime&amp;#039;) ?&amp;gt; &lt;span&gt;*&lt;\/span&gt;&lt;\/label&gt;&lt;br \/&gt; &lt;input&nbsp; id=&rdquo;firstname&rdquo; name=&rdquo;firstname&rdquo; class=&rdquo;input-text required-entry&rdquo;\/&gt; &lt;label for=&quot;&rdquo;lastname&rdquo;&quot;&gt; &amp;lt; ?php echo $this-&amp;gt;__(&amp;#039;Priimek&amp;#039;) ?&amp;gt; &lt;span&gt;*&lt;\/span&gt;&lt;\/label&gt;&lt;br \/&gt; &lt;input&nbsp; id=&rdquo;lastname&rdquo; name=&rdquo;lastname&rdquo;&hellip;&lt;input type=&quot;hidden&quot; name=&quot;trp-form-language&quot; value=&quot;sl&quot;\/&gt;<\/p>","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[40],"tags":[83,82,80,43],"class_list":["post-445","post","type-post","status-publish","format-standard","hentry","category-magento","tag-custom-form-validation","tag-form-validation","tag-frontend","tag-magento"],"_links":{"self":[{"href":"https:\/\/dknzdesign.com\/sl\/wp-json\/wp\/v2\/posts\/445","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dknzdesign.com\/sl\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dknzdesign.com\/sl\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dknzdesign.com\/sl\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/dknzdesign.com\/sl\/wp-json\/wp\/v2\/comments?post=445"}],"version-history":[{"count":8,"href":"https:\/\/dknzdesign.com\/sl\/wp-json\/wp\/v2\/posts\/445\/revisions"}],"predecessor-version":[{"id":453,"href":"https:\/\/dknzdesign.com\/sl\/wp-json\/wp\/v2\/posts\/445\/revisions\/453"}],"wp:attachment":[{"href":"https:\/\/dknzdesign.com\/sl\/wp-json\/wp\/v2\/media?parent=445"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dknzdesign.com\/sl\/wp-json\/wp\/v2\/categories?post=445"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dknzdesign.com\/sl\/wp-json\/wp\/v2\/tags?post=445"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}