{"id":104,"date":"2017-01-24T03:37:12","date_gmt":"2017-01-24T03:37:12","guid":{"rendered":"http:\/\/codeinsightacademy.com\/blog\/?p=104"},"modified":"2017-05-13T05:28:22","modified_gmt":"2017-05-13T05:28:22","slug":"form-field-validation","status":"publish","type":"post","link":"https:\/\/codeinsightacademy.com\/blog\/angular-js\/form-field-validation\/","title":{"rendered":"form field validation"},"content":{"rendered":"<p>The problem is that your REGX pattern will only match the input &#8220;0-9&#8221;.<\/p>\n<p>To meet your requirement (0-9999999), you should rewrite your regx pattern:<\/p>\n<p><code>ng-pattern=\"\/^[0-9]{1,7}$\/\"<\/code><\/p>\n<p>HTML:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"html\">&lt;div ng-app ng-controller=\"formCtrl\"&gt;\r\n    &lt;form name=\"myForm\" ng-submit=\"onSubmit()\"&gt; \r\n        &lt;input type=\"number\" ng-model=\"price\" name=\"price_field\" ng-pattern=\"\/^[0-9]{1,7}$\/\" required&gt; \r\n        &lt;span ng-show=\"myForm.price_field.$error.pattern\"&gt;Not a valid number!&lt;\/span&gt; \r\n        &lt;span ng-show=\"myForm.price_field.$error.required\"&gt;This field is required!&lt;\/span&gt; \r\n        &lt;input type=\"submit\" value=\"submit\"\/&gt; \r\n    &lt;\/form&gt; \r\n&lt;\/div&gt; \r\n<\/pre>\n<p>JS:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\">function formCtrl($scope){ \r\n    $scope.onSubmit = function(){ \r\n        alert(\"form submitted\"); \r\n    } \r\n}<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><code><br \/>\n<\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The problem is that your REGX pattern will only match the input &#8220;0-9&#8221;. To meet your requirement (0-9999999), you should rewrite your regx pattern: ng-pattern=&#8221;\/^[0-9]{1,7}$\/&#8221; HTML: &lt;div ng-app ng-controller=&#8221;formCtrl&#8221;&gt; &lt;form name=&#8221;myForm&#8221; ng-submit=&#8221;onSubmit()&#8221;&gt; &lt;input type=&#8221;number&#8221; ng-model=&#8221;price&#8221; name=&#8221;price_field&#8221; ng-pattern=&#8221;\/^[0-9]{1,7}$\/&#8221; required&gt; &lt;span ng-show=&#8221;myForm.price_field.$error.pattern&#8221;&gt;Not a valid number!&lt;\/span&gt; &lt;span ng-show=&#8221;myForm.price_field.$error.required&#8221;&gt;This field is required!&lt;\/span&gt; &lt;input type=&#8221;submit&#8221; value=&#8221;submit&#8221;\/&gt; &lt;\/form&gt; &lt;\/div&gt; JS: function [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[6],"tags":[],"_links":{"self":[{"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/posts\/104"}],"collection":[{"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/comments?post=104"}],"version-history":[{"count":6,"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/posts\/104\/revisions"}],"predecessor-version":[{"id":287,"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/posts\/104\/revisions\/287"}],"wp:attachment":[{"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/media?parent=104"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/categories?post=104"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/tags?post=104"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}