AngularJS - 表单自定义验证 - 检查是否至少有一个输入是空的 [英] AngularJS - Form Custom Validation - Check if at least one input is empty

查看:270
本文介绍了AngularJS - 表单自定义验证 - 检查是否至少有一个输入是空的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<form name="myForm" action="#sent" method="post" ng-app>
   <input name="userPreference1" type="text" ng-model="shipment.userPreference" />
   <input name="userPreference1" type="text" ng-model="shipment.userPreference" />
   <input name="userPreference1" type="text" ng-model="shipment.userPreference" />
... submit input and all the other code...
</form>

我需要你的帮助,以了解如何检查验证时间,如果输入至少有一个是空的。所需的验证如下。用户必须完成的至少一个preference

I need your help to know how to check on validation time, if at least one of the inputs is empty. The desired validation is the following. The user must complete at least one a preference.

使用jQuery这样的:

Using jQuery this:

if ( $("input").val() == "" ) {

工程确定,而是想弄清楚如何使用角度做同样的事情。

Works ok, but would like to figure out how to do the same thing using angular.

在此先感谢这么多,

吉列尔莫·

推荐答案

这样的想法是禁用的提交按钮,如果所有输入都是空白的。你可以这样做

So the idea is to disable the submit button if all inputs are blank. You can do like this

<form name="myForm" action="#sent" method="post" ng-app>
   <input name="userPreference1" type="text" ng-model="shipment.userPreference1" />
   <input name="userPreference1" type="text" ng-model="shipment.userPreference2" />
   <input name="userPreference1" type="text" ng-model="shipment.userPreference3" />

   <button type="submit" ng-disabled="!(!!shipment.userPreference1 || !!shipment.userPreference2  || !!shipment.userPreference3)">Submit</button>
</form>

!! STR 被强制转换 STR 布尔值。而且两者 !!空 !!被评估为

!!str is to force to convert str to a boolean value. And both !!null and !!"" are evaluated to be false.

<大骨节病> 演示

这篇关于AngularJS - 表单自定义验证 - 检查是否至少有一个输入是空的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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