AngularJS上只能提交验证 [英] AngularJS validation on submit only

查看:102
本文介绍了AngularJS上只能提交验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要实现我的AngularJS应用一些简单的表单验证,但我不希望它显示任何验证错误,直到用户点击了提交表单按钮后。我不希望它验证为I型,甚至在退出该领域。

I want to implement some simple form validation in my AngularJS app, but I don't want it to show any validation errors until after the user has clicked the form submit button. I don't want it to validate as I type or even on exiting the field.

有没有办法做到这一点?我需要写至少有一个自定义的验证指令,所以它需要与合作。

Is there a way to do this? I'll need to write at least one custom validator directive so it will need to work with that.

我发现在AngularJS表单验证是非常困难的,到目前为止,很难得到它正是你想要的工作。

I am finding form validation in AngularJS to be very difficult so far, it's hard to get it to work exactly as you want.

推荐答案

您可以做这样的事情。这里有一个例子

You could do something like this. Here's an example

<form name="form" ng-app>
    <div class="control-group" ng-class="{true: 'error'}[submitted && form.email.$invalid]">
            <label class="control-label" for="email">Your email address</label>
            <div class="controls">
                <input type="email" name="email" ng-model="email" required />
                <span class="help-inline" ng-show="submitted && form.email.$error.required">Required</span>
                <span class="help-inline" ng-show="submitted && form.email.$error.email">Invalid email</span>
            </div>
        </div>

        <button type="submit" class="btn btn-primary btn-large" ng-click="submitted=true">Submit</button>
    </form>

这篇关于AngularJS上只能提交验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆