jQuery Validate方法检查特殊字符

编程入门 行业动态 更新时间:2024-10-09 12:29:15
本文介绍了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");

推荐答案

包括从 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方法检查特殊字符

本文发布于:2023-11-27 18:42:11,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1639156.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:特殊字符   方法   jQuery   Validate

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!