2

I want to make the "other" field required ONLY if the selected text in the "select1" field is "other" the rule I'm trying is: other: { required: function(element){ return $("#select1 option:selected").text() == "Other"; } } Am i doing something wrong? Here's the code:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
                "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
  <script type="text/javascript" src="http://jzaefferer.github.com/jquery-  validation/jquery.validate.js"></script>
  <script>
  $(document).ready(function(){
    $('form:first').validate({
        rules: {
          cname: {
          required: true,
        },
        select1: { valueNotEquals: "0" }
        },
        other: {
                required: function(element){
                            return $("#select1 option:selected").text() == "Other";
                        }
        },   
        messages: {
          cname: "Please enter a valid naaaammmeeee.",
      select1: "Please choose a valid option",
      other: "Please enter a valid other value",
        },
      });

       $.validator.addMethod("valueNotEquals", function(value, element, arg){
        return arg != value;
        }, "Value must not equal arg.");
  });
  </script>

</head>
<body>


 <form class="cmxform" id="commentForm" method="get" action="">
 <fieldset>
   <legend>A simple comment form with submit validation and default messages</legend>
   <p>
     <label for="cname">Name</label>
     <em>*</em><input id="cname" name="cname"/>
   </p>
   <p>
   <select name="select1" id="select1">
    <option value="0">Please choose a value</option>
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">Other</option>
    </select>
    </p>
    <p>
    <label for="other">Other</label>
        <em>*</em><input id="other" name="other"/>
   </p>
       <p>
       <input class="submit" type="submit" value="Submit"/>
       </p>
 </fieldset>
 </form>
</body>
</html>
1
  • What exactly you want to achieve here? Can you be more specific please? Commented Dec 18, 2012 at 18:18

4 Answers 4

1

There were multiple problems mostly related to JSON formatting:

  1. one extra closing bracket here:

    select1: { valueNotEquals: "0" }
        },
    
  2. One closing bracket missing here at the end of the rules definition and just before the start of messages definition

    return $("#select1 option:selected").text() == "Other"; } } },
    messages: {

  3. Extra comma at end of each options were un-necessary

  4. Space in the validate.js path (this might be a typo here in the question itslef)

    here is the corrected code:

...

    <script src="http://code.jquery.com/jquery-latest.js"></script>
      <script type="text/javascript" src="http://jzaefferer.github.com/jquery-validation/jquery.validate.js"></script>
      <script>
        $(document).ready(function(){
    $('form:first').validate({
    rules: {
        cname: { required: true },
        select1: { valueNotEquals: "0" },
        other: { required: function(element){
                            return $("#select1 option:selected").text() == "Other";
                            }
            }
},   
        messages: {
            cname: "Please enter a valid naaaammmeeee.",
            select1: "Please choose a valid option",
            other: { required: "Please enter a valid other value"}
            }
});
...
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks Amar. That worked a treat. I'm relatively new to JQuery, and haven't gotten to grips with all the syntax rules just yet. Your help is much appreciated.
Cheers man :) Now, you should accept the answer and make it a habit to do so in future too if you feel satisfied with the answer. I am saying in general :)
Sorry Amar, though the validation is now working, my messages are being ignored, and the default "This field is required" is coming through. Is there something else missing here?
try it now. I have edited the code for now, need to change the explanation a bit, in the mean time you test the code above.
just back on this today. No that didn't seem to work. Cheers.
|
1

updated the answer find a demo at fiddle here: http://jsfiddle.net/3jrTM/

$('form:first').validate({
rules: {
    cname: {
        required: true
    },
    select1: {
        valueNotEquals: "0"
    },
    other: {
        required: function(element) {
            return $("#select1 option:selected").text() == "Other";
        }
    },
},
messages: {
    cname: "Please enter a valid naaaammmeeee.",
    select1: "Please choose a valid option",
    other: "Please enter a valid other value"
}

});

$.validator.addMethod("valueNotEquals", function(value, element, arg) {
return arg != value;
}, "Value must not equal arg.");​

2 Comments

Thanks Jai. That part is working. It's the following rule that's giving me problems.
Sorry Jai, I was away for a day. Thanks you very much for your answer.
0

You can delete the value of your option which contains the "0".

<option>Select here someting</option>

When a option contains no value atrribute, than jQuery validate handles this as empty or not given.

<select class="required"><option>Select here someting</option></select>

You can add 'required' as class to your selectbox so jQuery validate will promt a "required" warning.

Comments

0
$('select').change(function() {
   var text = $('select option:selected').text();​​​​​​​​​
   if(text == 'Other') {
       $('input[name=other]').attr('disabled', false);        
    }
  });

​is this what you mean

1 Comment

Apologies if the description is unclear. I want to make the "other" field required ONLY if the selected text in the "select1" field is "other" the rule I'm trying is: other: { required: function(element){ return $("#select1 option:selected").text() == "Other"; } },

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.