AngularJs form validation methods
Oct 19, 2017 am 09:25 AMAnjularjs Form Validation
It is very important to be able to give real-time visual feedback based on what the user enters in the form. In the context of human-to-human communication, the feedback given by form validation is as important as obtaining correct input.
Form validation can not only provide useful feedback to users, but also protect our web applications from being damaged by malicious or incorrect input. We have to do our best to protect the backend on the web frontend.
AngularJS can combine the HTML5 form validation function with its own validation instructions, and it is very convenient. AngularJS provides many form validation instructions.
1 2 3 |
|
To use form validation, first make sure the form has a name attribute like the example above.
All input fields can be subject to basic validation, such as maximum and minimum length, etc. These capabilities are provided by new HTML5 form attributes.
If you want to block the browser’s default validation behavior for the form, you can add the novalidate tag to the form element
1. Required
To verify whether a form input has been filled in, just add the HTML5 tag required on the input field element:
Note: The required attribute applies to the following <input> types :text, search, url, telephone, email, password, date pickers, number, checkbox, radio and file
1 |
|
2. Minimum length ng -minleng="{number}"
To verify whether the text length entered in the form is greater than a certain minimum value, use the AngularJS directive ng-minleng="{number}"
# on the input field
##
1 |
|
3. Maximum length ng-maxlength="{number}"
Verify whether the text length entered in the form is less than or equal to a certain maximum length value, use the AngularJS directive ng-maxlength="{number}"1 |
|
on the input field 4. Pattern matching ng-pattern="/PATTERN/"
Use ng-pattern="/PATTERN/" to ensure that the input matches the specified regular expression:1 |
|
5 . Email
To verify whether the input content is an email, just set the input type to email as follows:1 |
|
6. Numbers
Verify whether the input content is a number and set the input type to number:1 |
|
7 . URL
Verify whether the input content is a URL, set the input type to url:1 |
|
Control variables in the form
The properties of the form can be accessed in the $scope object to which they belong, and we can access the $scope object, so JavaScript can indirectly access the form properties in the DOM. With the help of these properties, we can make real-time (like everything else in AngularJS) responses to the form. These properties include the following. (Note that these properties can be accessed using the format below.) formName.inputFieldName.property1 |
|
1 |
|
1 |
|
1 |
|
1 |
|
$parsers
When the user interacts with the controller, and the ngModelController When the $setViewValue() method is called, the functions in the $parsers array will be called one by one in a pipeline. After the first $parse is called, the execution result will be passed to the second $parse, and so on These functions can convert the input value, or set the validity of the form through the $setValidity() function . Using the $parsers array is one of the ways to implement custom validation.例如,假设我们想要确保输入值在某两个数值之间,可以在 $parsers 数组中入栈一个新的函数,这个函数会在验证链中被调用。
每个 $parser 返回的值都会被传入下一个 $parser 中。当不希望数据模型发生更新时返回undefined 。
html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
|
javascript ( test5app.js )
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
|
The above is the detailed content of AngularJs form validation methods. For more information, please follow other related articles on the PHP Chinese website!

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

How to write a novel in the Tomato Free Novel app. Share the tutorial on how to write a novel in Tomato Novel.

How to delete WeChat friends? How to delete WeChat friends

How to enter bios on Colorful motherboard? Teach you two methods

How to recover deleted contacts on WeChat (simple tutorial tells you how to recover deleted contacts)

Summary of methods to obtain administrator rights in Win11

Quickly master: How to open two WeChat accounts on Huawei mobile phones revealed!

Detailed explanation of Oracle version query method

The secret of hatching mobile dragon eggs is revealed (step by step to teach you how to successfully hatch mobile dragon eggs)
