我正在尝试使用 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 将对输入中的文本做出正确 react ,但我不知道如何在此验证中使用比较来强制电子邮件在允许用户提交表单之前相同。
我希望有一个没有自定义指令的解决方案,但如果没有它就无法实现,我会处理它。 Here是使用自定义指令解决类似问题的答案。
感谢任何帮助,谢谢
最佳答案
您应该能够使用 ng-pattern/regex 来比较 2 个输入值
Email:<input type="email" name="email1" ng-model="emailReg">
Repeat Email:<input type="email" name="email2" ng-model="emailReg2" ng-pattern="emailReg">
并验证:
<span ng-show="registerForm.email2.$error.pattern">Repeat Email should have the same value with email!</span>
关于forms - 使用 AngularJS 比较表单验证中的两个输入值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22173016/