JQuery form validation – How to remove special characters!
i was using a jquery form validation plugin and it was so easy in validating a form. I was stuck in a scenario where i want users to register userID and i dont want them to enter special characters like dots and just want them to enter alphabets, number and underscore in the username.
I went through the documentation of the plugin and there was no such methods to strip the special characters in the plugin.
Finally, i somehow got hold of the code that does the job through regular expression regex that checks for only numbers and a-z
<script>
$(document).ready(function(){
$.validator.addMethod("noSpecialChars", function(value, element) {
return this.optional(element) || /^[a-z0-9\_]+$/i.test(value);
}, "Username must contain only letters, numbers, or underscore.");
$("#myForm").validate();
});
</script>
All you have to do is call this function from the textbox element. Dont forget to check out the example of addMethod()
//call this from html code within class=required <input id="cname" name="name" size="25" class="required noSpecialChars" minlength="2" />
Works like a charm!
Similar Posts:
- Quick & Easy Form Validation Tutorial with JQuery
- Fix ->Wordpres plugin could not be activated fatal error cannot redeclare!
- How URL shortening scripts work?
- Single/double quotes causes error in mysql INSERT query!
- how to call a php script from javascript?
- Fix ->Problem displaying foreign language characters in PHP
- Fix-> Warning: ereg_replace(): REG_BADRPT in PHP
- Affiliate Hide – Free wordpress plugin to redirect affiliate links!
- Tutorial: How to write a WordPress Plugin?
- Fix -> Fatal error – Call to undefined function: screen_icon() in WP-Pagenavi Plugin


July 27, 2009
Its was very useful, and there was no waste in time to understand the explanation , thanks alot
September 3, 2009
Thank u. Very useful and easy to understand. Keep it simple!
October 27, 2011
Fucking Genius!!!