使用angularJS完成表单验证功能

使用angualrJS实现表单的验证功能
和原生js相比减少很多代码量,简化代码,提示信息 简单明了,便于修改和增加验证条件
<script>
    angular.module('myApp',['ngMessages']).controller('formController', function ($scope) {

        $scope.email = "";
        $scope.password = "";
        $scope.submitForm = function () {
            if($scope.form_1.$valid){
                alert('通过验证');
            }else{
                alert('未通过验证');
            }
        }

    })
</script>
<div class="container">
    <div class="row">
        <div class="col-md-6 col-md-offset-3">
            <form action="#" name="form_1" ng-controller="formController" ng-submit="submitForm()">
                <div class="form-group">
                    <label>账号:</label>

                    <input class="form-control" name="email" ng-model="email"
                           ng-minlength="8"
                           ng-maxlength="12" placeholder="请输入账号" required="required">
                    <span class="error" ng-show="form_1.password.$invalid">
                        <small ng-show="form_1.email.$error.minlength">账号长度最小为6</small>
                        <small ng-show="form_1.email.$error.required">请输入账号</small>
                        <small ng-show="form_1.email.$error.maxlength">账号长度最大为12</small>
                    </span>
                </div>

                <div class="form-group">
                    <label>密码:</label>

                    <input class="form-control" name="password" ng-model="password"
                            ng-minlength="10" ng-maxlength="20"
                            placeholder="请输入密码" required="required"
                            ng-pattern="/\w/">
                    <span class="error" ng-show="form_1.password.$invalid">
                        <small ng-show="form_1.password.$error.minlength">密码长度最小为10</small>
                        <small ng-show="form_1.password.$error.required">请输入密码</small>
                        <small ng-show="form_1.password.$error.maxlength">账号长度最大为20</small>
                        <small ng-show="form_1.password.$error.pattern">密码必须为字母数字下划线组合</small>
                    </span>

                </div>

                <div class="form-group">
                    <input type="submit" value="登录" class="btn btn-default">
                </div>

            </form>
        </div>
    </div>
</div>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值