Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery Validation plug-in custom error placement

Using the jQuery Validation plug-in for the following form:

<form id="information" method="post" action="#">              <fieldset>                 <legend>Please enter your contact details</legend>                 <span id="invalid-name"></span>                 <div id="id">                     <label for="name">Name: (*)</label>                     <input type="text" id="name" class="details" name="name" maxlength="50" />                 </div>                  <span id="invalid-email"></span>                 <div id="id">                     <label for="email">Email: (*)</label>                     <input type="text" id="email" class="details" name="email" maxlength="50" />                 </div>             </fieldset>             <fieldset>                 <legend>Write your question here (*)</legend>                 <span id="invalid-text"></span>                 <textarea  id="text" name="text" rows="8" cols="8"></textarea>               <div id="submission">                 <input type="submit" id="submit" value="Send" name="send"/>             </div>             <p class="required">(*) Required</p>             </fieldset>               </form> 

How can I place the errors inside the span tags? (#invalid-name, #invalid-email, #invalid-text)

I read the documentation about error placement but I did not get how it works. Is it possible to handle each single error and place it in the specified element?

Thank you

like image 639
Mirko Avatar asked Jun 03 '10 20:06

Mirko


People also ask

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

Java scriptready(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 in jQuery?

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.

What is jQuery form validation?

Validation in JQuery: Using JQuery, a form is validated on the client-side before it is submitted to the server, hence saves the time and reduce the load on the server. Form Validation means to validate or check whether all the values are filled correctly or not.

What is jQuery validation engine?

jQuery validation engine is a Javascript plugin aimed at the validation of form fields in the browser (IE 6-8, Chrome, Firefox, Safari, Opera 10). The plugin provides visually appealing prompts that grab user attention on the subject matter.


1 Answers

You can also manually add error labels in places you need them. In my particular case I had a more complex form with checkbox lists etc. where an insert or insert after would break the layout. Rather than doing this you can take advantage of the fact that the validation script will evaluate if an existing label tag exists for the specified field and use it.

Consider:

<div id="id">     <label for="name">Name: (*)</label>     <input type="text" id="name" class="details" name="name" maxlength="50" /> </div> 

Now add the following line:

<label for="name" class="error" generated="true"></label>

which is standard error label:

<div id="id">     <label for="name">Name: (*)</label>     <input type="text" id="name" class="details" name="name" maxlength="50" /> </div> <div id="id-error">     <label for="name" class="error" generated="true"></label> <div> 

jQuery will use this label rather than generating a new one. Sorry I could not find any official documentation on this but found other posts that came across this behaviour.

like image 80
Stefan Morrow Avatar answered Oct 11 '22 06:10

Stefan Morrow



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!