Reputation: 1705
I have TextBox on my page. I want to validating this textbox.
The rules are :
how to do using jquery in best way
Upvotes: 0
Views: 320
Reputation: 338158
The appropriate regex would be:
^[a-zA-Z0-9/#&^\- ]{0,20}$
Ask yourself if A-Z
is the right thing. You may be wanting to allow more than that (accented characters for example).
Use the JavaScript Unicode Block Range Creator to accommodate the regex for a broader set of allowed input. Check allowed characters with with the handy Javascript RegExp Unicode Character Class Tester.
As always, double check form values on the server side. Relying on JavaScript to do all input checking is dangerous.
For jQuery, have a look at the Valitator plugin, and use code along the lines of:
$.validator.addMethod(
"yourFormField",
function(value) {
return /^[a-zA-Z0-9/#&^\- ]{0,20}$/.test(value);
},
"Please enter a valid value."
);
Upvotes: 1