I need to exclude usage of single quote and double quotes in my textbox. if user enters text with qutation i need to show error. How can i do it with jquery.validate
2 Answers
$('#elm').validate(
{
valid: function(inputValue){
return !inputValue.match(/['"]+/);
}
});
5 Comments
Mohan Ram
i dont know how to use it. explain me.
alex
Won't that match if it does have quotes?
Macy Abbey
Good point alex, changed it. So that plugin lets you validate any input element on the page. The function above tells the plugin to call the anonymous function in order to determine if the element with the id of 'elm' is valid or not. You should replace 'elm' with the id of the element you are validating.
mu is too short
Your regex only needs the character class:
/['"]/. There's no need for one or more when one will do. It still works though.Mohan Ram
Ur Code is not understandable explain me by using addmethod
use regular expression like ^[a-zA-Z] This will accept only Characters from A-Z and a-z.
Here is the code: Add a Method for Regular expression .
$.validator.addMethod("regex",function(value,element,regexp){
var re= new RegExp(regexp);
return this.optional(element) || re.test(value);
},"Only Characters from A-z");
And then you need to add a rule on that field.
rules:{
username:{
required:true,
regex:"^[a-zA-Z]+$" //Only Characters
}
},
messages:{
username:{
required:"Mandatory"
}
},
thats it..