I am trying to achieve jQuery validation. The scenario is if a field is empty it will give a required message error. If not empty and input is invalid it will give a specific error message for each field. With my current code I am getting this error:
Uncaught TypeError: Object [object Object] has no method ‘val’
Any hints for solving this? Here is the fiddle: http://jsfiddle.net/PDjwZ/4/
if (!/^[a-zA-Z]+$/.test(name.val()) && name.val() != emptyerror) {
name.addClass("needsfilled");
name.val(nameerror);
}
Edited (after view fiddle):
It seems that you aren’t using
varwhen declaring your variables. I’ve edited the fiddle to changename = $("#name")tovar name = $("#name"). Seems like the variable name is conflicting with some other (global) variable.