简体   繁体   English

AngularJS - 正则表达式不应以空格开头和结尾,不应包含双引号 (")

[英]AngularJS - regular expression which should not start & end with space and should not consist double quotation (")

I have written ng-pattern for not to start and end with space.我写了 ng-pattern 不以空格开头和结尾。 And trying to match double quotation in $watch.并试图匹配 $watch 中的双引号。

But it is not working.但它不起作用。 ng-pattern is also not working ng-pattern 也不起作用

my ng-pattern is:我的 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 >

$watch in JS is: 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');
        }
    });

Is there any other way to do this?有没有其他方法可以做到这一点?

Thanks in advance !提前致谢 !

You can change your regex to like this ng-pattern='/^[^\\\\\\\\./:*?\\"<>|][^\\\\\\\\/:*?\\"<>|]{0,254}$/' in your html to prevent user from entering (") in input and form can throw an error saying it's an invalid input.您可以将正则表达式更改为ng-pattern='/^[^\\\\\\\\./:*?\\"<>|][^\\\\\\\\/:*?\\"<>|]{0,254}$/'在您的 html 中以防止用户在输入和表单中输入 (") 可能会引发错误,指出这是无效输入。

In angular ng-trim is by default set to true so any white spaces surrounding user input are removed before assigning to ng-model .在 angular ng-trim中默认设置为 true,因此在分配给ng-model之前,用户输入周围的任何空格都会被删除。

You don't need a watcher unless and until you want the input value to be (") free every time user tries to enter some value in input field.It's an unnecessary operation to validate every time user changes something.您不需要观察者,除非并且直到您希望每次用户尝试在输入字段中输入某个值时输入值都为 (") 空闲。每次用户更改某些内容时进行验证是不必要的操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 名称的正则表达式,只接受字母,两个单词之间只接受空格。 名称的开头或结尾不允许有空格 - Regular expression for name which accepts alphabets only and space only between two words. no space allowed at the start or at end of the name 正则表达式,以避免特殊字符,并且不应以数字开头 - Regular expression to avoid special characters and should not start with numbers 应该禁用angularjs提交表单中的开始日期结束日期验证 - Start date End date validation in angularjs submit form should be disabled AngularJs打印双引号 - AngularJs printing double quotation 你应该如何在AngularJS中定义常规函数 - How should you define regular functions in AngularJS 是否可以在AngularJS中使用双正则表达式(正则表达式)计算String? - Is it possible to evaluate a String with a double regular expression (regex) in AngularJS? 应该避免在AngularJS控制器中包含常规Javascript对象吗? - Should one avoid including regular Javascript objects in AngularJS controllers? 车辆登记号的正则表达式,不应包含特殊字符 - Regular expression for vehicle registration number that should exclude special characters 为什么AngularJS在HTML中打印双引号 - Why AngularJS prints double quotation in HTML 正则表达式在AngularJS中不起作用 - Regular expression is not working in AngularJS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM