-1

我写了 ng-pattern 以不以空格开头和结尾。并尝试匹配 $watch 中的双引号。

但它不起作用。ng-pattern 也不起作用

我的 ng 模式是:

<input type="text" class="form-control" ng-model="wifiResult.ssid" 
     name="ssid"
     ng-class="{'inputError' : errorMsg}"
     ng-minlength="1"
     ng-maxlength="32"
     ng-pattern="/^[^\s]+(\s+[^\s]+)*$/"
     required >

JS 中的 $watch 是:

$scope.$watch('wifiResult.ssid', function(scope){
        var regexToRestrict = /^["]+$/;

        if(scope){
            var pressChar       = scope.slice(-1);
            if(pressChar.match(regexToRestrict)){
                scope   = scope.slice(0,-1); // try to replace " to blank
            }
            else{
                console.log('not matched');
            }
        }       
        else{
            console.log('no scope');
        }
    });

有没有其他方法可以做到这一点?

提前致谢 !

4

1 回答 1

0

您可以在您的 html 中将您的正则表达式更改为这样ng-pattern='/^[^\\\\./:*?\"<>|][^\\\\/:*?\"<>|]{0,254}$/',以防止用户在输入中输入 ("),并且表单可能会抛出一个错误,说明这是一个无效输入。

In angularng-trim默认设置为 true ,因此在分配给之前删除用户输入周围的任何空白ng-model

您不需要观察者,除非并且直到您希望每次用户尝试在输入字段中输入某些值时输入值都是(“)免费的。每次用户更改某些内容时验证是不必要的操作。

于 2016-09-24T13:48:14.783 回答