jQuery Validate方法检查特殊字符 [英] jQuery Validate method checking for special characters

查看:448
本文介绍了jQuery Validate方法检查特殊字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我知道这可能被问过1000次,但我面临着严重的时间压力.如果您不想回答以下问题,并且能够将我指向上述问题的先前答案,我将不胜感激.

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.

类似地,有人可以告诉我为什么当我输入小写字母'abc'等时以下内容会报错吗?我感到困惑.

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

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");

推荐答案

包括从 演示: http://jsfiddle.net/YsAKx/

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

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

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

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

这篇关于jQuery Validate方法检查特殊字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆