{"id":17478,"date":"2015-02-19T16:42:53","date_gmt":"2015-02-19T11:12:53","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=17478"},"modified":"2017-08-03T15:58:28","modified_gmt":"2017-08-03T10:28:28","slug":"form-validations-using-angularjs","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/form-validations-using-angularjs\/","title":{"rendered":"Form Validations using AngularJs"},"content":{"rendered":"<p>There are lots of ways to validate a web page like HTML5 validations, but why not do it in a better interactive way by\u00a0using AngularJS. <a title=\"Building Intuitive Frontend Interfaces with AngularJS\" href=\"http:\/\/www.tothenew.com\/blog\/building-intuitive-frontend-interfaces-with-angularjs\/\">AngularJS provides us with several form properties<\/a> for validation, which helps in providing instant feedback in case validation is violated.<\/p>\n<p><strong>\u00a0 \u00a0 $valid:<\/strong> return true when input field is valid<strong>.<\/strong><\/p>\n<p><strong>\u00a0 \u00a0 $invalid:<\/strong> return true when input field is invalid.<\/p>\n<p><strong>\u00a0 \u00a0 $pristine:<\/strong> return true if input field has not been used yet.<\/p>\n<p><strong>\u00a0 \u00a0 $dirty:<\/strong> return true if input field has been used.<\/p>\n<p><strong>\u00a0 \u00a0 $error:<\/strong> Another useful property is the $error object that contains all of the validations \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0on a particular form whether they are valid or invalid.<\/p>\n<p><strong>NOTE :<\/strong> The form must have a name associated with it to validate.<\/p>\n<p>Before using angular validations, we need to disable default validations provided by HTML5. For this, add <strong>novalidate<\/strong>\u00a0attribute in our form tag.<\/p>\n<p>[js]<br \/>\n&lt;form name=&quot;userForm&quot; novalidate&gt;<br \/>\n&#8230;<br \/>\n&lt;\/form<br \/>\n[\/js]<\/p>\n<p>Some Angular directives also provides validation (i.e ngRequired, ngMaxlength, ngPattern, ngTrim, ngMinlength, etc).<\/p>\n<p>Lets use\u00a0<strong>ngClass<\/strong> directive to set CSS dynamically\u00a0on invalid input field :<\/p>\n<p>[js]<br \/>\n&lt;div class=&quot;form-group&quot;&gt;<br \/>\n &lt;label for=&quot;uMobile&quot;&gt;Mobile&lt;\/label&gt;<br \/>\n &lt;input type=&quot;text&quot; ng-model=&quot;uMobile&quot; required ng-class=&quot;{ &#8216;has-error&#8217; : userForm.uMobile.$invalid &amp;&amp; !userForm.uMobile.$pristine }&quot; class=&quot;form-control&quot; id=&quot;uMobile&quot; name=&quot;uMobile&quot; ng-pattern=&quot;\/^[0-9]+$\/&quot; ng-minlength=&quot;10&quot; ng-maxlength=&quot;10&quot; placeholder=&quot;Mobile Number&quot;&gt;<br \/>\n &#8230;<br \/>\n&lt;\/div&gt;<\/p>\n<p>[\/js]<\/p>\n<p>Then, we\u00a0can also show the error message for each of our\u00a0inputs if they are invalid or\u00a0have already been used with<strong> ng-show<\/strong> directive.<\/p>\n<p>[js]<\/p>\n<p>&lt;p ng-show=&quot;userForm.uMobile.$error.minlength&quot; class=&quot;error&quot;&gt;Mobile number is too short.&lt;\/p&gt;<br \/>\n&lt;p ng-show=&quot;userForm.uMobile.$error.maxlength&quot; class=&quot;error&quot;&gt;Mobile number is too long.&lt;\/p&gt;<br \/>\n&lt;p ng-show=&quot;userForm.uMobile.$error.pattern&quot; class=&quot;error&quot;&gt;Enter a numeric value&lt;\/p&gt;<\/p>\n<p>[\/js]<\/p>\n<p><strong>\u00a0<\/strong>Atlast to disable submit button if our form is $invalid,<strong>\u00a0ng-disabled <\/strong>is used\u00a0.<\/p>\n<p>[js]<\/p>\n<p>&lt;button type=&quot;submit&quot; class=&quot;btn btn-info btn-primary&quot; ng-disabled=&quot;userForm.$invalid&quot;&gt; Submit&lt;\/button&gt;<\/p>\n<p>[\/js]<\/p>\n<p>So in this way we can make use of <a title=\"Angular Development\" href=\"http:\/\/www.tothenew.com\/front-end-angularjs-development\">Angular validations<\/a> for validating our webpages easily. For demo please visit my github page <a title=\"Angular Validations\" href=\"https:\/\/github.com\/sandeepchhapola\/angular-validations\">link<\/a>\u00a0.<\/p>\n<p><span style=\"font-size: 20px; color: blue;\">Read Further on AngularJS Series<\/span><\/p>\n<ul>\n<li><a href=\"http:\/\/www.tothenew.com\/blog\/angularjs-a-write-less-do-more-javascript-framework\/\">AngularJS :\u00a0A \u201cWrite Less Do More\u201d JavaScript Framework<\/a><\/li>\n<li><a href=\"http:\/\/www.tothenew.com\/blog\/angularjs-updating-a-label-dynamically-with-user-input\/\">AngularJS :\u00a0Updating a Label Dynamically with User Input<\/a><\/li>\n<li><a href=\"http:\/\/www.tothenew.com\/blog\/angularjs-adding-items-to-a-javascript-list-and-updating-corresponding-dom-dynamically\/\">AngularJS :\u00a0Adding Items to a JavaScript List and updating corresponding DOM Dynamically<\/a><\/li>\n<li><a href=\"http:\/\/www.tothenew.com\/blog\/angularjs-text-suggestions\/\">AngularJS :\u00a0Text Suggestions<\/a><\/li>\n<li><a href=\"http:\/\/www.tothenew.com\/blog\/angularjs-sorting-objects-on-various-attributes\/\">AngularJS :\u00a0Sorting objects on various attributes<\/a><\/li>\n<li><a href=\"http:\/\/www.tothenew.com\/blog\/angularjs-fetching-data-from-the-server\/\">AngularJS :\u00a0Fetching data from the server<\/a><\/li>\n<li><a href=\"http:\/\/www.tothenew.com\/blog\/angularjs-multiple-views-layout-template-and-routing\/\">AngularJS :\u00a0Multiple Views, Layout\u00a0Template\u00a0and Routing<\/a><\/li>\n<li><a href=\"http:\/\/www.tothenew.com\/blog\/angularjs-implementing-routes-and-multiple-views\/\">AngularJS :\u00a0Implementing Routes And Multiple Views<\/a><\/li>\n<li><a href=\"http:\/\/www.tothenew.com\/blog\/building-intuitive-frontend-interfaces-with-angularjs\/\">Building Intuitive Frontend Interfaces with AngularJS<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>There are lots of ways to validate a web page like HTML5 validations, but why not do it in a better interactive way by\u00a0using AngularJS. AngularJS provides us with several form properties for validation, which helps in providing instant feedback in case validation is violated. \u00a0 \u00a0 $valid: return true when input field is valid. [&hellip;]<\/p>\n","protected":false},"author":137,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":0},"categories":[1185],"tags":[3955,1186,4697,55,1647],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/17478"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/137"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=17478"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/17478\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=17478"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=17478"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=17478"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}