kratos2333
2/15/2017 - 3:41 AM

Form Validation

Form Validation

Required
To validate that a form input has been filled out, we simply add the HTML5 tag, required, to the
input field:
<input type="text" required />
Minimum Length
To validate that a form input input is at least a certain {number} of characters, we add the AngularJS
directive ng-minlength="{number}" to the input field:
<input type="text" ng-minlength=5 />
Maximum Length
To validate that a form input is equal to or less than a number of characters, we can add the AngularJS
directive ng-maxlength="{number}":
<input type="text" ng-maxlength=20 />
Matches a Pattern
To ensure that an input matches a regex, we can use ng-pattern="/PATTERN/":
<input type="text" ng-pattern="/a-zA-Z/" />
Email
To validate an email address in an input field, we simply set the input type to email, like so:
<input type="email" name="email" ng-model="user.email" />

Number
To validate an input field has a number, we set the input type to number:
<input type="number" name="age" ng-model="user.age" />
URL
To validate that an input represents a URL, set the input type to url:
<input type="url" name="homepage" ng-model="user.facebook_url" />