我正在尝试(并且失败)编写一个正则表达式语句,以我的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");推荐答案
包括从 additional-methods.js 文件,并使用alphanumeric规则.
$(document).ready(function () { $('#myform').validate({ rules: { field: { alphanumeric: true } } }); });演示: jsfiddle/YsAKx/
Demo: jsfiddle/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方法检查特殊字符
发布评论