Angularjs   发布时间:2022-04-20  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了AngularJs表单验证实例1大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

代码

<h1>Validation Example</h1>
<form class="form-horizontal container" ng-app="myApp" ng-controller="validateCtrl"
        name="myForm" novalidate>
    <div class="form-group">
        <label class="control-label">用户名:</label>
        <input class="form-control" name="user" type="text" ng-model="user" required />
        <span class="text-danger" ng-show="myForm.user.$dirty && myForm.user.$invalid">
            <span ng-show="myForm.user.$error.required">用户名是必须的</span>
        </span>
    </div>
    <div class="form-group">
        <label class="control-label">邮箱:</label>
        <input class="form-control" name="email" type="email" ng-model="email" required />
        <span class="text-danger" ng-show="myForm.email.$dirty && myForm.email.$invalid">
            <span ng-show="myForm.email.$error.required">邮箱是必须的</span>
            <span ng-show="myForm.email.$error.email">邮箱格式不正确</span>
        </span>
    </div>
    <p>
        <button class="btn btn-success" ng-disabled="myForm.user.$dirty && myForm.user.$invalid ||
            myForm.email.$dirty&&myForm.email.$invalid">
            确定提交
        </button>
    </p>
</form>
<script>
    var app = angular.module('myApp',[]);
    app.controller('validateCtrl',function ($scope) {
        $scope.user = 'admin';
        $scope.email = "100@qq.com";
    });
</script>

结果:


说明:

1.客户端的验证不能确保用户输入数据的安全,所以服务端的数据验证也是必须的。

2.HTML 表单属性novalidate用于禁用浏览器认的验证。

实例解析

AngularJSng-model指令用于绑定输入元素到模型中。

模型对象有两个属性useremail

我们使用了ng-show指令, color:red 在邮件$dirty$invalid显示

$prisTine
属性 描述
$dirty 表单有填写记录
$valid 字段内容合法的
$invalid 字段内容是非法的
表单没有填写记录

大佬总结

以上是大佬教程为你收集整理的AngularJs表单验证实例1全部内容,希望文章能够帮你解决AngularJs表单验证实例1所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。