【发布时间】:2014-04-06 01:13:09
【问题描述】:
我正在尝试使用 AngularJS 进行表单验证。我对比较两个值特别感兴趣。我希望用户在继续之前确认他输入的一些数据。假设我有以下代码:
<p>
Email:<input type="email" name="email1" ng-model="emailReg">
Repeat Email:<input type="email" name="email2" ng-model="emailReg2">
<p>
然后我可以使用验证:
<span ng-show="registerForm.email1.$error.required">Required!</span>
<span ng-show="registerForm.email1.$error.email">Not valid email!</span>
<span ng-show="emailReg !== emailReg2">Emails have to match!</span> <-- see this line
registerForm.$valid 将对输入中的文本做出正确反应,除非我不知道如何在此验证中使用比较来强制电子邮件相同,然后才允许用户提交表单。
我希望有一个没有自定义指令的解决方案,但如果没有它就无法实现,我会处理它。 Here 是使用自定义指令解决类似问题的答案。
感谢您的帮助,谢谢
【问题讨论】:
-
提交按钮上的
ng-disabled="emailReg != emailReg2"怎么样?
标签: forms angularjs validation