0

I am using jQuery Validation Plugin for form validation and I am not able to validate the select box using not the value fields but the text field..

Here my situation:

[HTML]

<form id="form">
      <select name="customPrice[0][50]" id="customPrice050">
                <option value="2435">Select date... </option>
                <option value="474">Friday, Mar 1st</option>
                <option value="475">Friday, Mar 8th</option>
                <option value="476">Friday, Mar 15th</option>
      </select>
      <br/>
      <input type="submit" value="Click" />

[jQuery]

$(document).ready(function()
{       
 // add the rule here
 $.validator.addMethod("valueNotEquals", function(value, element, arg){
  return arg != value;
 }, "Value must not equal arg.");

 // configure your validation
 $("form").validate({
  rules: {
   "customPrice[0][50]": { valueNotEquals: "2435" }
  },
  messages: {
   "customPrice[0][50]": {
    valueNotEquals: "Please select a date!"
   }
  }  
 });

 }

);

This works very well. The problem is that the value 2435 in not static and it always changes. The only static property is the text "Select date... ". Now I would something like:

"customPrice[0][50]": { textNotEquals: "Select date... " }

Can someone help me?

2
  • 1
    if you are using a recent jQuery, you could make a rule that checks the selected index of your select is greater than 0 $(...).prop("selectedIndex") > 0 stackoverflow.com/a/7373474/678338 Commented Mar 13, 2013 at 14:58
  • @politus - That's a great idea. Much better than depending on the text of the first option. Commented Mar 13, 2013 at 16:05

1 Answer 1

1

The middle argument to the callback function is the element being validated. You should be able to use that to get the text from the selected option and check that it is not "Select date...".

Try this:

$.validator.addMethod("selectTextNotEquals", function(value, select, arg) {
    return arg != $(select).find('option:selected').text();
}, "Text must not equal arg.");

DEMO

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

Comments

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.