我有一個字段,也需要自定義驗證
以下指令
app.directive('postcode', function() {
return {
require: 'ngModel',
link: function(scope, elm, attrs, ctrl) {
ctrl.$parsers.unshift(function (val) {
var regex = /^(([gG][iI][rR] {0,}0[aA]{2})|((([a-pr-uwyzA-PR-UWYZ][a-hk-yA-HK-Y]?[0-9][0-9]?)|(([a-pr-uwyzA-PR-UWYZ][0-9][a-hjkstuwA-HJKSTUW])|([a-pr-uwyzA-PR-UWYZ][a-hk-yA-HK-Y][0-9][abehmnprv-yABEHMNPRV-Y]))) {0,}[0-9][abd-hjlnp-uw-zABD-HJLNP-UW-Z]{2}))$/,
valid = regex.test(val);
if(valid) {
ctrl.$setValidity('postcode', true);
return val;
}
else {
ctrl.$setValidity('postcode', false);
return undefined;
}
});
}
};
});
雖然所需的位無縫工作(耶!) 作品之一,也是郵政編碼顯示所需的錯誤信息以及無效
(由於某種原因,複製/粘貼DOM元素會導致一個錯誤,但犯規說什麼)