forms - 使用 AngularJS 比较表单验证中的两个输入值

标签 forms angularjs validation

我正在尝试使用 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/

相关文章:

javascript - 工厂中的 AngularJS $http 请求

ruby-on-rails - Rails 嵌套属性 : require at least two records

Angular 2模板表单验证不起作用

PHP 在刷新时不断插入

php - 唯一的表单 token 禁用用户的多任务处理

Django - ChoiceFieldcleaned_data 获取字符串而不是整数

javascript - 在 ionic 中使用 ng-click 时,类延迟更改为 'activated'

javascript - $locationChangeStart 仅在后退按钮上

javascript - 使用用户选择的 xml 文件填写表单

c - 如果用户输入非数字字符,如何仅扫描整数并重复读取?