-1

I have added errors manually to my input using the link in here Here and the example in Here but when I try $("#myshowErrors").valid() after I added the errors it becomes true?

here is my code

var validator = $( "#myshowErrors" ).validate();
validator.showErrors({
  "firstname": "I know that your firstname is Pete, Pete!"
});

I am not able to make the client validation fail. How can I make form.valid() to return false?

I don't want to make form.valid()=false; manually I want that to be taken care of by just setting the errors.

Helen Araya
  • 1,886
  • 3
  • 28
  • 54

1 Answers1

0

It's entirely unclear by the lack of code in your question, but I think you might misunderstand where to attach the .valid() method.

This method only gets attached to a selector representing a single <form> element...

$('#myform').valid();  // triggers validation on the entire form

Or to any selector that represents a single form input element...

$('#myform input[name="myinput"]').valid();  // triggers validation on one input element

When .valid() is called, validation is triggered on the selected element, error message(s) is/are displayed, and the method will return true or false.

The .valid() method can never be attached to a selector that represents more than one element, or a collection of elements without also using a jQuery .each().


EDITS:

showErrors is just for showing existing errors. You cannot "invalidate" a field by calling showErrors. Fields are either valid or invalid based solely on what is contained within that field.


You've tagged the question with Unobtrusive Validation. Since jQuery Validation is handled automatically via the Unobtrusive Validation plugin, you are not allowed to call your own instance of the .validate() method. That's because the Unobtrusive plugin automatically constructs and calls .validate(). Once it's called, the plugin is initialized, it cannot be called again, and all subsequent calls are always ignored.

Sparky
  • 98,165
  • 25
  • 199
  • 285
  • So my question is if I invalidate an input my self using validator.showErrors({ "firstname": "I know that your firstname is Pete, Pete!" }); why would $('#myform').valid() return true. I want it to return to false if I invalidate any one inout from my form. – Helen Araya Jun 26 '17 at 17:09
  • @AmeteBlessed, because you made a mistake someplace. I cannot help you since I cannot see your code. – Sparky Jun 26 '17 at 17:10
  • @AmeteBlessed, I also have no idea why you think you can "invalidate" a field by calling `showErrors`. Fields are either valid or invalid based solely on what is contained within that field. – Sparky Jun 26 '17 at 17:13
  • So what would be the best way to invalidate a field on the fly? – Helen Araya Jun 26 '17 at 17:14
  • @AmeteBlessed, that is not possible. Fields are valid/invalid based solely on what the user puts inside and I have no idea why you'd need to do such a thing. Now if you want to change rules on the fly, that's another story. – Sparky Jun 26 '17 at 17:19