Home Web Front-end JS Tutorial About the implementation code analysis of AngularJS verification form function

About the implementation code analysis of AngularJS verification form function

May 26, 2017 am 10:19 AM
angularjs form validation

This article mainly introduces the AngularJS form verification function, and analyzes the operating steps, implementation skills and related precautions of AngularJS form verification based on specific examples. #, Friends who need it can refer to

This article describes the AngularJS form verification function with examples. Share it with everyone for your reference, the details are as follows:

Under the jurisdiction of AngularJS, each form form will create an instance of ngFormController. Each input in the form will create an ngModelController instance under this instance to control the behavior of each input.

First start with simple verification.

AngularJs's ngModelController provides several properties:

$pristine;$dirty;$valid;$invalid ;$error

pristine: The form has not changed.
dirty: The form has changed.
valid: All controls comply with validation rules.
invalid: At least one control does not comply with the validation rules.
error: There is an error, but I don’t know what is wrong.

Create a form as follows:

<form name="userForm" method="#" action="#">
  <label for="name">用户名1</label>
  <input id="name" name="name" type="text" ng-model="users.name" pattern="^\w{6,18}$" required>
  <span ng-show="userForm.name.$error.pattern">用户名必须XXXX</span>
</form>
Copy after login

First, each form must have its own name. Use this name to control the input below you. Then, each

input tag must have its own name. Name is a critical step and is used to identify each different input and thereby identify different ngModelControllers. Use pattern to set your own regular expression rules. Use $error to check whether the regular expression is correct. And assign it to the ng-show directive. If there is an error, the entire $error will return true and the set prompt information will be displayed until the regular expression passes, $error will return false, and ng-show will hide it.

The above simple example does not reset the

controller behavior yourself, everything is the default of AngularJs. You can see that the judgment code in ng-show can actually be completed by JS later, so you can see the specific process of ngModelController.

<!--html-->
<form name="userForm" ng-controller="main" method="#" action="#">
  <label for="name">用户名1</label>
  <input id="name" name="name" type="text" ng-model="users.name" pattern="^\w{6,18}$" required>
  <span style="color:red" ng-show="showError(userForm.name,&#39;pattern&#39;)">用户名必须XX</span>
  <span style="color:green" ng-show="showSuccess(userForm.name)">成功!</span>
  <!--对按钮进行动态锁定-->
  <button class="btn btn-primary btn-lg" ng-disabled="submit(userForm)">SAVE</button>
</form>
Copy after login
/*JS*/
app.controller("main",function($scope){
  $scope.showError=function(ngModelController,abc){
    return ngModelController.$error[abc];
  };
  $scope.showSuccess=function(ngModelController){
    return ngModelController.$valid; /*至少有1错误,返回false,无错误,返回true*/
  };
   $scope.submit=function(ngFormController){
    return ngFormController.$invalid; /*valid的取反*/
  };
});
Copy after login

The judgment is made directly in the ngModelController in the controller. There is a pitfall in it, that is, there are no parameters for judging attributes such as $valid. To judge $error, you need to indicate "what to judge". I passed a regular expression of my own to him.

AngularJs can also "lock" the submit button of the form. But it should be noted that at this time, the Controller corresponding to BUTTON is no longer the ngModelController for a certain input, but the ngFormController for the entire form. Therefore, the $invalid inside is to determine whether there is any problem with all the inputs, and everything is fine. will be unlocked when .

Thinking: Can regular expressions or minlength similar restrictions be written in JS files.

The above is the detailed content of About the implementation code analysis of AngularJS verification form function. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use Flask-WTF to implement form validation How to use Flask-WTF to implement form validation Aug 03, 2023 pm 06:53 PM

How to use Flask-WTF to implement form validation Flask-WTF is a Flask extension for handling web form validation. It provides a concise and flexible way to validate user-submitted data. This article will show you how to use the Flask-WTF extension to implement form validation. Install Flask-WTF To use Flask-WTF, you first need to install it. You can use the pip command to install: pipinstallFlask-WTF import the required modules in F

Laravel Development: How to validate form requests using Laravel Validation? Laravel Development: How to validate form requests using Laravel Validation? Jun 13, 2023 pm 01:34 PM

Laravel is a popular PHP web development framework that provides many convenient features to speed up the work of developers. Among them, LaravelValidation is a very practical function that can help us easily validate form requests and user-entered data. This article will introduce how to use LaravelValidation to validate form requests. What is LaravelValidationLaravelValidation is La

How to implement form validation for web applications using Golang How to implement form validation for web applications using Golang Jun 24, 2023 am 09:08 AM

Form validation is a very important link in web application development. It can check the validity of the data before submitting the form data to avoid security vulnerabilities and data errors in the application. Form validation for web applications can be easily implemented using Golang. This article will introduce how to use Golang to implement form validation for web applications. 1. Basic elements of form validation Before introducing how to implement form validation, we need to know what the basic elements of form validation are. Form elements: form elements are

How to use CodeIgniter4 framework in php? How to use CodeIgniter4 framework in php? May 31, 2023 pm 02:51 PM

PHP is a very popular programming language, and CodeIgniter4 is a commonly used PHP framework. When developing web applications, using frameworks is very helpful. It can speed up the development process, improve code quality, and reduce maintenance costs. This article will introduce how to use the CodeIgniter4 framework. Installing the CodeIgniter4 framework The CodeIgniter4 framework can be downloaded from the official website (https://codeigniter.com/). Down

PHP form validation tips: How to use the filter_input function to verify user input PHP form validation tips: How to use the filter_input function to verify user input Aug 01, 2023 am 08:51 AM

PHP form validation tips: How to use the filter_input function to verify user input Introduction: When developing web applications, forms are an important tool for interacting with users. Correctly validating user input is one of the key steps to ensure data integrity and security. PHP provides the filter_input function, which can easily verify and filter user input. This article will introduce how to use the filter_input function to verify user input and provide relevant code examples. one,

How to handle form validation using middleware in Laravel How to handle form validation using middleware in Laravel Nov 02, 2023 pm 03:57 PM

How to use middleware to handle form validation in Laravel, specific code examples are required Introduction: Form validation is a very common task in Laravel. In order to ensure the validity and security of the data entered by users, we usually verify the data submitted in the form. Laravel provides a convenient form validation function and also supports the use of middleware to handle form validation. This article will introduce in detail how to use middleware to handle form validation in Laravel and provide specific code examples.

Form validation and filtering methods in PHP? Form validation and filtering methods in PHP? Jun 29, 2023 pm 10:04 PM

PHP is a scripting language widely used in web development, and its form validation and filtering are very important parts. When the user submits the form, the data entered by the user needs to be verified and filtered to ensure the security and validity of the data. This article will introduce methods and techniques on how to perform form validation and filtering in PHP. 1. Form validation Form validation refers to checking the data entered by the user to ensure that the data complies with specific rules and requirements. Common form verification includes verification of required fields, email format, and mobile phone number format.

The latest 5 angularjs tutorials in 2022, from entry to mastery The latest 5 angularjs tutorials in 2022, from entry to mastery Jun 15, 2017 pm 05:50 PM

Javascript is a very unique language. It is unique in terms of the organization of the code, the programming paradigm of the code, and the object-oriented theory. The issue of whether Javascript is an object-oriented language that has been debated for a long time has obviously been There is an answer. However, even though Javascript has been dominant for twenty years, if you want to understand popular frameworks such as jQuery, Angularjs, and even React, just watch the "Black Horse Cloud Classroom JavaScript Advanced Framework Design Video Tutorial".

See all articles