JDesigns
JDesigns

Reputation: 2332

jquery validation

I am using this http://docs.jquery.com/Plugins/validation and I am having a situation where I need to show an error message when one of the option in the dropdown is selected. For xample, if I have 8 options in the drop down, and if they select the 4th option, I want to show an error message. I couldn't find any demo with this validation check..please let me know how I should approach this. Should be adding any custom "addmethod" to do this and then define the error messages etc..thanks.

Upvotes: 1

Views: 195

Answers (1)

Trufa
Trufa

Reputation: 40717

This is a new update answer based on the comments, as far as I understood this is exactly what you needed, I made it as simple ass possible.

HTML:

<div id="err"></div>
<form>
  <select class="target">
    <option value="1" selected="selected">Option 1</option>
    <option value="2">Option 2</option>
    <option value="3">Option 3</option>
  </select>
</form>

CSS:

#err{ height:20px;}

jQuery:

$('.target').change(function() {
  if ($(".target").val() == "2"){
    $('#err').append('Error!');
  }else{
   $('#err').empty();
  }
});

See a working example here.

Is this what you were needing?

This answer addresses exactly that issue with an example.

Good luck!

I've updated the example to fit your example's needs. See here.

Upvotes: 1

Related Questions