Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery validation trigger error message

I have a kinda wierd problem. I want to trigger a jquery-valdation errormessage on an element even thou it's valid.

Scenario: I have a large form. One of the inputs is for PersonalId. It's optional to enter that PersonalId. Beside that input i have a normal button (not the submit-button). If you click on that (fetching information from ajax) and the PersonalId field is empty i want to trigger that Error-message.

Is that possible with jQuery Validate or do i need to create my own function?

like image 512
mannge Avatar asked Apr 27 '12 11:04

mannge


People also ask

How can I show error message below the textbox in jQuery validation?

Java script ready(function() { $("#basic-form"). validate(); }); This is based on the assumption that you have already added the required JavaScript files. Adding those lines of JavaScript will make sure that your form is properly validated and shows all the error messages.

What is Errorplacement?

Definition of jQuery validate errorplacement. The jQuery validate errorplacement() function is used to customize the placement of an error message. This function is a built-in function in jQuery. These function changes the default behavior of the validation plugin to insert the error label after the input fields.


1 Answers

Is that possible with jQuery Validate or do i need to create my own function?

Both, sort of. You can use the showErrors function to display errors on the form manually, but since you don't want this to be a rule that is enabled when you submit the form, you'll have to do your own checking for the value in the input.

Something like:

var $validator = $("#myform").validate(); $("#checkid").click(function() {     var errors;      if (!$("#personalid").val()) {         /* Build up errors object, name of input and error message: */         errors = { personalid: "Please enter an ID to check" };         /* Show errors on the form */         $validator.showErrors(errors);                 }         }); 

Example: http://jsfiddle.net/andrewwhitaker/XjZer/

Note that the form will still submit if there is nothing in the personalid field.

like image 132
Andrew Whitaker Avatar answered Sep 23 '22 07:09

Andrew Whitaker