Use RegEx In JQuery Validate Plugin To Ensure No Illegal Characters In Folder Name
I'm trying to use a RegEx to ensure that users don't put any illegal Windows characters in their folder name that gets created (Yes, I check server-side before creating the folder as well), and I'd like to use JQuery's Validate plugin (http://docs.jquery.com/Plugins/Validation/) to do this, but all the RegEx examples seem to point me toward making sure that what the user enters MATCHES, rather than DOES 开发者_StackOverflowNOT MATCH. Can anyone point me in the right direction as to what I'm doing wrong? Thanks!
Here's my JQuery Code (ironically, it works in reverse - if I enter one of the illegal characters, it's fine, but if I take it out, it triggers the validation):
$(document).ready(function() {
$.validator.addMethod(
"regex",
function(value, element) {
return this.optional(element) || /:|\?|\\|\*|\"|<|>|\||%/g.test(value);
},
"Asserter Names Cannot Contain Certain Characters.");
$('#matterDetail').validate({
debug: true,
rules: {
"Asserter": {
required: true,
regex: true,
minlength: 2
}
}
});
});
I got the regex rule from here: http://RegExr.com?2rj55
You can just use !() to reverse your rule...
return this.optional(element) || !(/:|\?|\\|\*|\"|<|>|\||%/g.test(value));
精彩评论