0
寫作angularjs定製指令某些輸入驗證像不允許輸入有超過10個字符,不允許輸入有開頭或結尾空格,寫下面的代碼Angularjs自定義指令驗證
angular.module("angulareDir", [])
.directive("myDirective", function() {
return{
restrict: 'A',
link: function($scope, elem, attrs) {
var reg = /^\s+|\s+$/g;
angular.element(elem).on("keypress", function(e) {
var eMessage = angular.element("#message-error");
if (elem.val().length >= 10) {
e.preventDefault();
}
if (reg.test(elem.val()) === true) {
eMessage.show();
e.preventDefault();
}
});
}
};
});
它工作正常的輸入具有超過10個字符的值,並且如預期的那樣,不允許用戶在10個字符後鍵入,但它不適用於前導/尾隨空格,我不確定我的正則表達式是否錯誤?
使用情況不爲我工作的時候我之前和喜歡
空間空間空間字符串後一直空格的「Hello World」的空間空間空間
注:我只想要允許用戶在字符串之間有空格,但不在字符串的開始和結尾處,我想在用戶開始在輸入框中輸入時檢查此內容
即使它不起作用 – zumuhu