gpt4 book ai didi

javascript - 使用 angularjs 的电话格式自定义指令

转载 作者:行者123 更新时间:2023-11-28 07:56:59 25 4
gpt4 key购买 nike

我正在尝试使用 angularjs 为美国电话号码编写自定义指令,并且需要将字段的数据类型保留为整数。这是 jsfiddle directive 并需要帮助来完成该指令。

如果用户输入有效的电话号码(正好 10 个号码,即 1234567890),那么当用户移动到下一个控件时,输入应分为 3 个 block ,如 123-456-7890。否则我应该显示错误消息“不是有效号码” ”。

<div ng-app="myApp" ng-controller="myCtrl">
<form name="myForm">
<input type="text" ng-model="telephone" phoneformat name="input1" />
<span class="error" ng-show="myForm.input1.$error.telephone">Numbers only!</span>
<span class="error" ng-show="myForm.input1.$error.telephone">Exact 10 Numbers only!</span>

</form>
</div>

var myApp = angular.module("myApp", []);

var myCtrl = myApp.controller("myCtrl",["$scope", function($scope) {
$scope.telephone = "1234567890";
}]);

myApp.directive("phoneformat", function () {
return {
restrict: "A",
require: "ngModel",
link: function (scope, element, attr, ngModelCtrl) {
var phoneformat = function () {

}

}
};
});

最佳答案

您似乎想利用表单的 $error 属性来驱动验证。为此,您需要在指令中所需的 ngModelCtrl 中调用 $setValidity:

var myApp = angular.module("myApp", []);

var myCtrl = myApp.controller("myCtrl",["$scope", function($scope) {
$scope.telephone = "1234567890";
}]);

myApp.directive("phoneformat", function () {
return {
restrict: "A",
require: "ngModel",
link: function (scope, element, attr, ngModelCtrl) {
//Parsing is performed from angular from view to model (e.g. update input box)
//Sounds like you just want the number without the hyphens, so take them out with replace
var phoneParse = function (value) {
var numbers = value && value.replace(/-/g, "");
if (/^\d{10}$/.test(numbers)) {
return numbers;
}

return undefined;
}
//Formatting is done from view to model (e.g. when you set $scope.telephone)
//Function to insert hyphens if 10 digits were entered.
var phoneFormat = function (value) {
var numbers = value && value.replace(/-/g,"");
var matches = numbers && numbers.match(/^(\d{3})(\d{3})(\d{4})$/);

if (matches) {
return matches[1] + "-" + matches[2] + "-" + matches[3];
}

return undefined;
}

//Add these functions to the formatter and parser pipelines
ngModelCtrl.$parsers.push(phoneParse);
ngModelCtrl.$formatters.push(phoneFormat);

//Since you want to update the error message on blur, call $setValidity on blur
element.bind("blur", function () {
var value = phoneFormat(element.val());
var isValid = !!value;
if (isValid) {
ngModelCtrl.$setViewValue(value);
ngModelCtrl.$render();
}

ngModelCtrl.$setValidity("telephone", isValid);
//call scope.$apply() since blur event happens "outside of angular"
scope.$apply();
});
}
};
});

工作 fiddle 。这只是演示 ngModel 中使用的解析器和格式化程序管道的快速方法,以及用于填充 $error 字段的 $setValidity。

更新:要在多部手机上使用相同的手机验证,请使用带有 $error 的表单。请注意,每个输入都有一个与 myForm(表单名称)一起使用的唯一名称。两者都使用 $error.telephone:

<form name="myForm">
Mobile Phone:
<input type="text" ng-model="mobilephone" phoneformat name="mobilephone" />
<span class="error" ng-show="myForm.mobilephone.$error.telephone">
Exact 10 Numbers only!
</span>
<br />
Home Phone:
<input type="text" ng-model="homephone" phoneformat name="homephone" />
<span class="error" ng-show="myForm.homephone.$error.telephone">
Exact 10 Numbers only!
</span>
</form>

更新了fiddle

关于javascript - 使用 angularjs 的电话格式自定义指令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25979540/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com