码迷,mamicode.com
首页 > Web开发 > 详细

angular js input校验只能输入数字和字母 directive写法一则

时间:2016-09-09 18:44:32      阅读:234      评论:0      收藏:0      [点我收藏+]

标签:

js代码:
.directive(‘validateNumberLetter‘, function () {
return {
require: ‘ngModel‘,
link: function (scope, elm, attrs, ctrl) {
scope.$watch(attrs.ngModel, function(n){
if(!n) return;
var oldName = attrs.validateNumberLetter;
if(oldName !== n){
var reg=/[^A-Za-z0-9]/;
if (n.length) {
if(reg.test(n)){
// it is invalid, return undefined (no model update)
ctrl.$setValidity(‘validateNumberLetter‘, false);
return undefined;
}else{
// it is valid
ctrl.$setValidity(‘validateNumberLetter‘, true);
return n;
}
}
}
});
}
};
})
html代码:
<div class="form-group-right">
<input required validate-number-letter
    id="name"
name="name"
class="form-control verify-input"
type="text"
ng-model="volume.name"
ng-maxlength="255"
ng-disabled="context.mode === ‘extend‘ ||
context.mode === ‘attach‘ ||
context.mode === ‘detach‘"
placeholder="{% trans ‘enter a volume name‘ %}"/>
<p class="help-block alert-fixed alert-danger"
ng-show="form.name.$error.required && form.name.$dirty">
{% trans "A volume name is required." %}
</p>
<p class="help-block alert-fixed alert-danger"
ng-show="form.name.$error.maxlength && form.name.$dirty">
{% trans "Name length should be less than 255 chars." %}
</p>
<p class="help-block alert-fixed alert-danger"
ng-show="form.name.$error.validateNumberLetter && form.name.$dirty">
{% trans "should be letter" %}
</p>
</div>

angular js input校验只能输入数字和字母 directive写法一则

标签:

原文地址:http://www.cnblogs.com/braveliuchina/p/5857500.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!