Home >Web Front-end >JS Tutorial >AngularJS implements form validation_AngularJS
Although I am not a front-end programmer, I understand how important it is to do verification on the front-end.
Because this way the backend can take a breather, and compared to the backend, the frontend can actually improve the user's happiness.
AngularJS provides a very convenient form validation function, which is recorded here.
First start with the following code
Required
Length
Specific format
For example, email, URL, number, just set type to the corresponding type, for example:
Use directive ng-pattern, for example:
pristine/dirty
Indicates whether it has been modified, such as
AngularJS provides corresponding css class for form status
For example, make verification pass green and failure red:
In the example given, only one email verification is written. If you add several fields, several different prompts, and several events, the code will become messy.
This is actually not recommended, we have a better way.
Just use angular-messages.js
First of all, don’t forget these two steps
Okay, first replace those duplicates with ng-messages and ng-message. The above example becomes: