方案一:ng-blur事件,正则去空格
<input type="tel" id="mobileInpt" class="f14" name="mobile" value="" placeholder="请输入您的手机号" ng-model="vm.mobile" ng-blur="vm.mobileRE($event)" required>
vm.mobileRE = function($event) {
  var that = $event.currentTarget;
  $(that).val(function(n, c) {
     return c.replace(/(^\s*)|(\s*$)/g, "");
  });
}

 

方案二:指令,keyup事件
<input type="tel" id="mobileInpt" class="f14" name="mobile" value="" placeholder="请输入您的手机号" ng-model="vm.mobile" space-filter="mobileRE" required>
angular.module('app').directive('spaceFilter', [function() {
  return {
    require: "ngModel",
    link: function(scope, element, attrs, ngModel) {
      var attr = attrs.spaceFilter;
      if (attr) {
         var dataType = {
          "mobileRE": /\s*/g
          }
        var regex = dataType[attr];
      }
      element.bind('keyup', function(value) {
        this.value = this.value.replace(regex, '');
     });
   }
 }
}])

 

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-02-19
  • 2021-12-31
  • 2022-12-23
  • 2023-03-19
猜你喜欢
  • 2021-08-02
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-01-03
  • 2022-12-23
相关资源
相似解决方案