1

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.

1

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

7 Comments

thanks this helps but i am wondering if there is a way to show the message inline immediately after the option is selected. I have the message showing up in two clicks i.e choose option from dropdown and then click outside to see the message..but i want the error message to show up right after the option is selected. and also when the other option is chosen, it should remove that error message.
@Jay: There are many ways to do that, how do you want the error to be displayed exactly? As a a text warning? BTW I don't see the sense in giving an error message when an option is selected, I would rather just deactivate that option (show it but unselectable is generally more useful to the user). I working on an example though on what you want.
I can't work on this for a while but will continue afterwards, if you want to attempt it yourself, this could come in handy api.jquery.com/val/#val1 I when I have time I will continue, see this too jsfiddle.net/2QAA8/13 if you want a place to start... Good luck!
thanks..this is what i kind of want. if you go here jquery.bassistance.de/validate/demo/errorcontainer-demo.html and click on the "submit" on the second form, you can see the error messages appear. now if you select an option from drop down(color), the message disappears and also if you again select "blank" option, the message appears again..so i am looking do something like this. so in my case, i am on a page where all fields are filled, then selecting an option from dropdown will show a message(without ciicking anything). if you choose other option, the message hides. thanks.
@Jay: I've updated my answer and hope that it fills your needs!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.