[英]ng-Pattern in Angular
Users enter username here and username is just number 用户在此处输入用户名,用户名只是数字
<fieldset class="col-sm-12 col-xs-12 text-center">
<label for="username">Username</label>
<input type="text" id="username" ng-pattern="/^[0-9]*$/"
class="text-warning"
ng-class="{'text-left': form.userName.$valid}"
autocomplete="off"
ng-model="self.user.userName"
name="userName"
ng-required="true"
ng-blur="self.user.usernamePlaceholder = false"
ng-focus="self.user.usernamePlaceholder = true"
maxlength="20"
onfocus="if (this.hasAttribute('readonly')) { this.removeAttribute('readonly');this.blur(); this.focus() }" />
</fieldset>
But ng-pattern dosent work 但是ng模式的剂量工作
I can type anything 我可以输入任何东西
When we use ng-pattern
in fact we set input validation for that input. 实际上,当我们使用
ng-pattern
,我们会为该输入设置输入验证。
for example: 例如:
<input type="text" ng-pattern="/^[0-9]*$/" ng-model="test" />
{{test}}
This input already validate by
ng-pattern
that mean when i want to type test123 return empty to me and when type 123 will return 123 , so your codes is fine.此输入已经通过
ng-pattern
了验证,这意味着当我想要输入test123时,我将返回空值 ,而当输入123时将返回123 ,因此您的代码很好。
But what you want is not pattern it's directive , i write custom directive ngInteger
: 但是,您想要的不是模式它的指令 ,而是编写自定义指令
ngInteger
:
var app = angular.module("app", []) app.directive('ngInteger', function(){ return { require: 'ngModel', link: function(scope, element, attrs, modelCtrl) { modelCtrl.$parsers.push(function (inputValue) { // this next if is necessary for when using ng-required on your input. // In such cases, when a letter is typed first, this parser will be called // again, and the 2nd time, the value will be undefined if (inputValue == undefined) return '' var transformedInput = inputValue.replace(/[^0-9]/g, ''); if (transformedInput!=inputValue) { modelCtrl.$setViewValue(transformedInput); modelCtrl.$render(); } return transformedInput; }); } }; });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <div ng-app="app"> <form name="form"> <h3>ng-pattern</h3> <input type="text" name="pattern" ng-pattern="/^[0-9]*$/" ng-model="test1" /> {{test1}} <p>test 1 validation: {{form.pattern.$valid}}</p> <h3>ng-integer</h3> <input type="text" name="integer" ng-integer ng-model="test2" /> {{test2}} <p>test 2 validation: {{form.integer.$valid}}</p> </form> </div>
Not necessary to use ng-pattern
,if you are using directive. 如果使用指令,则不必使用
ng-pattern
。 It actually does the same thing on keypress
itself. 实际上,它本身在
keypress
也做同样的事情。
valid-number here is directive mentioned below. Mention the `maxlength` if needed.
In HTML: 在HTML中:
<input type="text" valid-number maxlength="12" ng-model="test" />
Use this directive in input
tag 在
input
标签中使用此指令
var myApp = angular.module("myApp", []);
myApp.directive('validNumber', function() {
return {
require: '?ngModel',
link: function(scope, element, attrs, ngModelCtrl) {
if(!ngModelCtrl) {
return;
}
ngModelCtrl.$parsers.push(function(val) {
if (angular.isUndefined(val)) {
var val = '';
}
var clean = val.replace( /[^0-9]+/g, '');
if (val !== clean) {
ngModelCtrl.$setViewValue(clean);
ngModelCtrl.$render();
}
return clean;
});
element.bind('keypress', function(event) {
if(event.keyCode === 32) {
event.preventDefault();
}
});
}
};
});
Hope this helps.. 希望这可以帮助..
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.