Angular form表单失去焦点校验

之前做了一个AngualrJs form表单自动校验,同事说用户体验很差,一输入就是错误,都不想注册了。。几番查资料修改后 。。。之前的代码就不看了,直接看修改后的---->



<!DOCTYPE html>
<html lang="en" ng-app="findPassword">
<head>
    <meta charset="UTF-8">
    <title></title>
    <link rel="stylesheet" href="css/bootstrap.css"/>
    <style>
        input{
            text-indent: 2em;
            border:none;
            outline: none;
        }
        .form-control-feedback{
            left: 0;
        }
        .text-error{
            color: #ff7e7e;
        }
        .submit{
            text-indent:0;
        }
        #findPassword{
            display: block;
        }
    </style>
    <script src="js/lib/jQuery-2.2.0.min.js"></script>
    <script src="js/lib/md5-min.js"></script>
    <script src="js/lib/angular.js"></script>
</head>
<body ng-controller="appCtrl">
<div class="modal" id="findPassword"  role="dialog" aria-labelledby="findPassword">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h3>重设密码</h3>
            </div>
            <div class="modal-body">
                <form  name="findPasswords" ng-submit="formSubmit(!findPasswords.password.$error.passwords && !findPasswords.passwordAgain.$error.pwmatch)" novalidate>
                    <div class="form-group">
                        <span ng-show="findPasswords.password.$error.passwords" class="text-error">
                            密码格式不正确
                        </span>
                        <span ng-show="findPasswords.passwordAgain.$error.pwmatch && !findPasswords.password.$error.passwords" class="text-error">
                             两次密码输入不一致
                        </span>
                    </div>
                    <div class="form-group has-feedback">
                        <span class="glyphicon glyphicon-lock form-control-feedback"></span>
                        <input type="password" class="form-control" name="password" id="userPassword" ng-model="user.password" placeholder="6-16个字符(英文字母或数字,区分大小写)" check-email-on-blur required />
                    </div>
                    <div class="form-group has-feedback">
                        <span class="glyphicon glyphicon-lock form-control-feedback"></span>
                        <input type="password" class="form-control" name="passwordAgain" ng-model="user.passwordAgain" pw-check="userPassword" placeholder="再次输入新密码" required/>
                    </div>
                    <div class="form-group">
                        <input type="submit" ng-click="allowValidation()" class="form-control btn btn-primary submit"  value="注册"/>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>
</body>
<script>
    (function () {
        'use strict';
        var app = angular.module('findPassword',[]);
        app.controller('appCtrl',['$scope', function ($scope) {
            $scope.user = {};
		
            $scope.formSubmit = function (isValid) {

                if(!isValid){
		//判断是否有错,如果有错,不执行
return false; } else{ console.log( $scope.user); } }; $scope.allowValidation = function () { $scope.$broadcast('kickOffValidations'); }; }]); app.directive('pwCheck', [function () {
		//校验前后密码是否一致
            return {
                restrict: 'A',
                require: "ngModel",
                link: function (scope, elm, attr, ctrl) {
                    if (attr.type === 'radio' || attr.type === 'checkbox') return;
                    elm.unbind('input').unbind('keydown').unbind('change');
                    elm.bind('blur', function () {
                        scope.$apply(dovalidation);
                    });
                    scope.$on('kickOffValidations', dovalidation);
                    function dovalidation(){
                        var firstPassword = '#' + attr.pwCheck;
                        if(elm.val() === $(firstPassword).val()){
                            ctrl.$setValidity('pwmatch', true);
                        }else{
                            ctrl.$setValidity('pwmatch', false);
                        }
                    }
                }
            }
        }]);

        app.directive('checkEmailOnBlur', function(){
		//校验密码格式
            var Password_REGX = /^\d{9,16}$|^(?!\d+$)[0-9A-z]{6,16}$/;
            return {
                restrict: 'A',
                require: 'ngModel',
                link: function(scope, elm, attr, ctrl) {
                    if (attr.type === 'radio' || attr.type === 'checkbox') return;
                    elm.unbind('input').unbind('keydown').unbind('change');
                    elm.bind('blur', function () {
                        scope.$apply(dovalidation);
                    });
                    scope.$on('kickOffValidations', dovalidation);
                    function dovalidation() {
                        if (Password_REGX.test(elm.val())) {
                            ctrl.$setValidity('passwords', true);
                        } else {
                            ctrl.$setValidity('passwords', false);
                        }
                    }
                }
            };
        });
     
    })();
</script>
</html>

以下是一个简单的 Angular 表单提交校验控制案例: HTML 代码: ``` <form (ngSubmit)="onSubmit()" #myForm="ngForm"> <div class="form-group"> <label for="name">Name:</label> <input type="text" class="form-control" id="name" name="name" [(ngModel)]="name" required minlength="3"> <div *ngIf="myForm.submitted && myForm.controls.name.errors" class="text-danger"> <div *ngIf="myForm.controls.name.errors.required"> Name is required. </div> <div *ngIf="myForm.controls.name.errors.minlength"> Name must be at least 3 characters long. </div> </div> </div> <div class="form-group"> <label for="email">Email:</label> <input type="email" class="form-control" id="email" name="email" [(ngModel)]="email" required email> <div *ngIf="myForm.submitted && myForm.controls.email.errors" class="text-danger"> <div *ngIf="myForm.controls.email.errors.required"> Email is required. </div> <div *ngIf="myForm.controls.email.errors.email"> Invalid email format. </div> </div> </div> <button type="submit" class="btn btn-primary" [disabled]="!myForm.valid">Submit</button> </form> ``` 在上面的代码中,我们定义了一个表单,其中包含两个输入框:一个用于输入名称,另一个用于输入电子邮件地址。我们使用了 `ngModel` 指令来绑定输入框的值到组件中的属性,使用 `required` 和 `minlength` 校验器来验证名称输入框是否符合要求,使用 `required` 和 `email` 校验器来验证电子邮件输入框是否符合要求。 在每个输入框下面,我们使用了一个 `*ngIf` 结构来显示校验错误信息。如果表单已经提交且存在相应的错误,则显示相应的错误信息。 最后,我们在提交按钮上使用了一个 `[disabled]` 属性来禁用按钮,以防止用户在表单无效时提交表单。 TypeScript 代码: ``` import { Component } from '@angular/core'; import { NgForm } from '@angular/forms'; @Component({ selector: 'app-form', templateUrl: './form.component.html', styleUrls: ['./form.component.css'] }) export class FormComponent { name: string; email: string; onSubmit() { console.log('Form submitted'); } } ``` 在组件中,我们定义了 `name` 和 `email` 两个属性来存储输入框的值。在 `onSubmit` 方法中,我们只是简单地打印一条消息,表示表单已经提交。 注意,我们使用了 `NgForm` 类型来注入表单对象,以便在模板中使用表单的各种属性和方法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值