Skip to content

Instantly share code, notes, and snippets.

@craigbutcher
Last active November 3, 2015 03:19
Show Gist options
  • Save craigbutcher/59752be0460c7776569f to your computer and use it in GitHub Desktop.
Save craigbutcher/59752be0460c7776569f to your computer and use it in GitHub Desktop.
jQuery Validation - No Special Characters allowed (not allowing < > # )
// Thanks to this site to help out on RegEx - http://www.regexr.com/
require(["jQueryValidate"], function(){
// dont allow < > # \ /
// add alphanumeric validator
jQuery.validator.addMethod("noSpecialCharacter", function(value, element) {
var regex = new RegExp("[<>#]+");
var key = value;
if (regex.test(key)) {
return false;
}
return true;
}, "please do not use the characters: < > #");
//login box
$('.loginBox form').validate({});
//advanced search
if (has('.advanced-search')) {
$('.advanced-search').validate({
rules: {
text: {
noSpecialCharacter: true
}
}
});
}
// feedback
$('.standard-validation').validate({
});
//register
if (has('#registration-form')) {
$('#registration-form').validate({
});
}
// site search
$('.search-validation form').each(function(){
$(this).validate({
rules: {
text: {
noSpecialCharacter: true
}
}
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment