jQuery Validate 方法检查特殊字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14949257/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 14:08:16  来源:igfitidea点击:

jQuery Validate method checking for special characters

jqueryregexjquery-validate

提问by timmackay

I am trying (and failing) to write a regular expression statement that checks for special characters such as !@#$%^&*()_+<>?'"{}[] in my Javascript form validation.

我正在尝试(但失败)编写一个正则表达式语句,用于在我的 Javascript 表单验证中检查特殊字符,例如 !@#$%^&*()_+<>?'"{}[]。

I understand that this has probably been asked 1000 times but i'm under some serious time pressure. If you would rather not answer the question below and you are able to point me in the direction of a previous answer to the above question I would greatly appreciate it.

我知道这可能已被问到 1000 次,但我面临着严重的时间压力。如果您不想回答下面的问题,并且您能够指出我之前回答上述问题的方向,我将不胜感激。

On a similar note, can anyone tell me why the following is shooting an error when I enter lowercase 'abc', etc? I'm baffled.

同样,有人能告诉我为什么当我输入小写的“abc”等时出现以下错误?我很困惑。

jQuery.validator.addMethod("specialChars", function( value, element ) {
        var regex = new RegExp("^[a-zA-Z0-9]+$");
        var key = String.fromCharCode(event.charCode ? event.which : event.charCode);

        if (!regex.test(key)) {
           event.preventDefault();
           return false;
        }
    }, "please use only alphanumeric or alphabetic characters");

回答by Sparky

Instead of writing your own custom method from scratch, include the additional-methods.jsfile and use the alphanumericrule.

不要从头开始编写自己的自定义方法,而是包含additional-methods.js文件并使用alphanumeric规则。

$(document).ready(function () {

    $('#myform').validate({
        rules: {
            field: {
                alphanumeric: true
            }
        }
    });

});

Demo: http://jsfiddle.net/YsAKx/

演示:http: //jsfiddle.net/YsAKx/



If you don't want to include an additional external file, simply copy the default alphanumericmethod out of it...

如果您不想包含额外的外部文件,只需从中复制默认alphanumeric方法...

jQuery.validator.addMethod("alphanumeric", function(value, element) {
    return this.optional(element) || /^\w+$/i.test(value);
}, "Letters, numbers, and underscores only please");

回答by Arun P Johny

Few changes

变化不大

jQuery.validator.addMethod("specialChars", function( value, element ) {
        var regex = new RegExp("^[a-zA-Z0-9]+$");
        var key = value;

        if (!regex.test(key)) {
           return false;
        }
        return true;
    }, "please use only alphanumeric or alphabetic characters");
  1. You need to return the value true, if the test is valid
  2. The validator is not a event handler so you cannot use event.preventDefault(), this has to be done in the keypressevent.
  3. You need to test the value passed to the validate method.
  1. 如果测试有效,您需要返回值 true
  2. 验证器不是事件处理程序,因此您不能使用event.preventDefault(),这必须在keypress事件中完成。
  3. 您需要测试传递给验证方法的值。

Demo: Fiddle

演示:小提琴

Note: Since you are using +wild char at least one character is required in the text box, you may want to split it into two rules using the requiredrule and change the wild char to *.

注意:由于您使用+通配符,文本框中至少需要一个字符,您可能希望使用required规则将其拆分为两个规则并将通配符更改为*

回答by Rathod Rajesh

^[a-zA-Z0-9;,.!@#$%:{}[]?"^&*()/\']*|[^&;<>\`]*

you want special charecter add here in this [] braces ^[a-zA-Z0-9;,.!@#$%:{}[]?"^&*()/\']and you dont want then add here this part [^&;<>\]`

您想在此 [] 大括号中添加特殊字符,^[a-zA-Z0-9;,.!@#$%:{}[]?"^&*()/\']而您不想在此处添加此部分[^&;<>\]`