表单验证角
问题描述:
我有几个字段在我的表单&我想启用提交按钮,如果他们中的任何人被填充,我该如何做到这一点?如果我要求两者或其中任何一个人,那么它将不会按我的意愿工作。表单验证角
<input type="text" name="phone">
<span ng-show="form.addContactForm.phone.$touched && form.addContactForm.phone.$error.required">Phone number or email is required</span>
<input type="text" name="email">
<span ng-show="form.addContactForm.email.$touched && form.addContactForm.email.$error.required">Phone number or email is required</span>
<button type="submit" ng-disabled="form.addContactForm.$invalid || form.addContactForm.$submitted">Submit</button>
如果输入的电话或电子邮件,然后其他的消息应该隐藏
答
你只需要添加的条件
<input type="text" name="phone" ng-model="ctrl.phone">
<span ng-show="(form.addContactForm.phone.$touched || form.addContactForm.email.$touched) && !ctrl.phone && !ctrl.email">Phone number or email is required</span>
<input type="text" name="email" ng-model="ctrl.email">
<span ng-show="(form.addContactForm.phone.$touched || form.addContactForm.email.$touched) && !ctrl.phone && !ctrl.email">Phone number or email is required</span>
<button type="submit" ng-disabled="(!ctrl.phone && !ctrl.email) || form.addContactForm.$invalid || form.addContactForm.$submitted">Submit</button>
编辑:添加错误消息和条件
答
<!DOCTYPE html>
<html>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<body>
<h2>Validation Example</h2>
<form ng-app="myApp" ng-controller="validateCtrl"
name="myForm" novalidate>
<p>Username:<br>
<input type="text" name="user" ng-model="user" required>
<span style="color:red" ng-show="myForm.user.$dirty && myForm.user.$invalid">
<span ng-show="myForm.user.$error.required">Username is required.</span>
</span>
</p>
<p>Email:<br>
<input type="email" name="email" ng-model="email" required>
<span style="color:red" ng-show="myForm.email.$dirty && myForm.email.$invalid">
<span ng-show="myForm.email.$error.required">Email is required.</span>
<span ng-show="myForm.email.$error.email">Invalid email address.</span>
</span>
</p>
<p>
<input type="submit"
ng-disabled="myForm.user.$dirty && myForm.user.$invalid ||
myForm.email.$dirty && myForm.email.$invalid">
</p>
</form>
<script>
var app = angular.module('myApp', []);
app.controller('validateCtrl', function($scope) {
$scope.user = 'John Doe';
$scope.email = '[email protected]';
});
</script>
</body>
</html>
+0
当您使用您未写入的代码时,请提供[您的来源](http://www.w3schools.com/angular/tryit.asp?filename=try_ng_validate)。 – Mistalis
答
最简单解决方案,如果此表单是您拥有的:使用ng-required
并使条件成为eac h字段依赖在其他领域。我甚至添加了一个ng-disabled
,这样,如果一个字段被填充,另一个被禁用。为了清晰起见,我没有包含错误消息 - 请使用您已有的<span>
。
<div ng-app="app" ng-controller="Ctrl as c">
<form name="contactForm">
<input ng-model="c.phone" type="text" name="phone"
ng-required="!c.email" ng-disabled="c.email" />
<input ng-model="c.email" type="text" name="email"
ng-required="!c.phone" ng-disabled="c.phone" />
<button ng-disabled="contactForm.$invalid">Submit</button>
</form>
</div>
相关小提琴:https://jsfiddle.net/k0L7c7jh/
如果模型变得更大,这样的(即验证直接对UI规则)的解决方案不能很好地扩展。为此,我喜欢模型验证,并为此创建了我在我的工作egkyron中使用的一个JS验证框架binds以及Angular(以及others)。
好的,谢谢我也需要显示信息,编辑我的问题,请检查 –