简体   繁体   English

当另一个输入改变时强制 ngPattern 验证

[英]Force ngPattern validation when another input is changed

I have an Angular JS application in which there is a required phone number field, as well as a country select field.我有一个 Angular JS 应用程序,其中有一个必填的电话号码字段,以及一个国家/地区选择字段。 The country select defaults to United States.国家选择默认为美国。 When the selected country is United States or Canada, I want to validate the phone number field with a domestic phone number pattern, but when any other country is selected, I don't want to validate the pattern for the phone number.当所选国家/地区是美国或加拿大时,我想用国内电话号码模式验证电话号码字段,但选择其他国家/地区时,我不想验证电话号码的模式。

To accomplish this desired functionality, I used a similar solution to this (the dirty way).为了实现这一点所需的功能,我使用了类似的解决方案,的方式)。 Instead of checking a requireTel boolean value, I check whether the countryCode is USA or CAN to determine whether to validate with my phone number pattern.我没有检查requireTel布尔值,而是检查countryCodeUSA还是CAN以确定是否使用我的电话号码模式进行验证。

This solution works, but only if you change the phone number after you change the country.此解决方案有效,但前提是您在更改国家/地区后更改电话号码。 For example, if you have United States selected, and type in a long international phone number, like +441234567890 , it uses the domestic phone number validation and shows Invalid pattern.例如,如果您选择了美国,并输入一个很长的国际电话号码,例如+441234567890 ,它将使用国内电话号码验证并显示无效模式。 But when I change the country to Bahamas, it should no longer use the domestic phone number validation, and the Invalid pattern message should go away.但是当我将国家更改为巴哈马时,它应该不再使用国内电话号码验证,并且无效模式消息应该消失。 However, the Invalid pattern message is still shown until I change the phone number.但是,在我更改电话号码之前,仍会显示无效模式消息。 Once I change the phone number, the message goes away.一旦我更改了电话号码,消息就会消失。

The reason this is happening is because the ng-pattern validation is not performed on the phone number field again when the country select is being changed.发生这种情况的原因是因为更改国家/地区选择时不会再次对电话号码字段执行 ng-pattern 验证。 Is there a way to force the ng-pattern validation to be re-evaluated whenever the country is changed?有没有办法在国家发生变化时强制重新评估 ng-pattern 验证?

See the snippet below to reproduce the issue.请参阅下面的代码段以重现该问题。

 var app = angular.module('example', []); app.controller('exampleCtrl', function($scope) { $scope.countries = [{ countryName: 'United States', countryCode: 'USA' }, { countryName: 'Canada', countryCode: 'CAN' }, { countryName: 'Bahamas', countryCode: 'BHS' }, { countryName: 'Chile', countryCode: 'CHL' }]; $scope.selectedCountry = $scope.countries[0]; $scope.phoneNumberPattern = (function() { var regexp = /^\\(?(\\d{3})\\)?[ .\\/-]?(\\d{3})[ .-]?(\\d{4})$/; return { test: function(value) { var countryCode = $scope.selectedCountry.countryCode; if (countryCode !== 'USA' && countryCode !== 'CAN') { return true; } return regexp.test(value); } } })(); });
 .error { color: red; font-style: italic; }
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <div ng-app="example" ng-controller="exampleCtrl"> <ng-form name="exampleForm"> <label>Country:</label> <select ng-model="selectedCountry" ng-options="country as country.countryName for country in countries"> </select> <label>Phone:</label> <input type="text" ng-model="phone" name="phone" ng-pattern="phoneNumberPattern" required> <small class="error" ng-show="exampleForm.phone.$error.pattern">Invalid pattern.</small> <small class="error" ng-show="exampleForm.phone.$error.required">Phone number is required.</small> </ng-form> </div>

Here's another solution.这是另一个解决方案。 Using a function that returns a pattern.使用返回模式的函数。

Live example on jsfiddle . jsfiddle上的实时示例。

 angular.module('ExampleApp', []) .controller('ExampleController', function($scope) { $scope.countries = [{ countryName: 'United States', countryCode: 'USA' }, { countryName: 'Canada', countryCode: 'CAN' }, { countryName: 'Bahamas', countryCode: 'BHS' }, { countryName: 'Chile', countryCode: 'CHL' }]; $scope.selectedCountry = $scope.countries[0]; var regUSA = /^\\(?(\\d{3})\\)?[ .\\/-]?(\\d{3})[ .-]?(\\d{4})$/; var regAll = /.*/; $scope.phoneNumberPattern = function() { var countryCode = $scope.selectedCountry.countryCode; if (countryCode !== 'USA' && countryCode !== 'CAN') return regAll; else return regUSA; } });
 .error { color: red; font-style: italic; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.8/angular.js"></script> <div ng-app="ExampleApp"> <div ng-controller="ExampleController"> <ng-form name="exampleForm"> <label>Country:</label> <select ng-model="selectedCountry" ng-options="country as country.countryName for country in countries"> </select> <label>Phone:</label> <input type="text" ng-model="phone" name="phone" ng-pattern="phoneNumberPattern()" required> <small class="error" ng-show="exampleForm.phone.$error.pattern">Invalid pattern.</small> <small class="error" ng-show="exampleForm.phone.$error.required">Phone number is required.</small> </ng-form> </div> </div>

NOTE: It is important that the regular expression variables are declared outside the function.注意:在函数外部声明正则表达式变量很重要。 If you try to change the method in the following way:如果您尝试按以下方式更改方法:

if (countryCode !== 'USA' && countryCode !== 'CAN')
    return /.*/;
else
    return /^\(?(\d{3})\)?[ .\/-]?(\d{3})[ .-]?(\d{4})$/;

It will lead to an infinite $digest error .它会导致无限的$digest error

Here's one way to address the problem.这是解决问题的一种方法。

What I've essentially done is added a ng-change="changed()" to the dropdown.我基本上所做的是在下拉列表中添加了一个ng-change="changed()"

<select ng-model="selectedCountry" ng-change="changed()" ng-options="country as country.countryName for country in countries">
    </select>

In the JS, I reset the value of the input text box在JS中,我重置了输入文本框的值

 $scope.changed=function()
  {
    $scope.phone="";
  }

This forces the validation to be performed again.这将强制再次执行验证。

Here is the DEMO .这是演示

Let me know if this solution works for you.让我知道此解决方案是否适合您。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM