开发者

jQuery custom validation for a selected radio selection

开发者 https://www.devze.com 2022-12-27 02:00 出处:网络
This is my requirement: I have a bunch of radio box selections (types of workflows). If one of the radios are selected(i.e one particular type of workflow selected), I want to run a custom validation

This is my requirement: I have a bunch of radio box selections (types of workflows). If one of the radios are selected(i.e one particular type of workflow selected), I want to run a custom validation on that. This is what i tried, but it's not behaving well. Any help?

jQuery part:

  $(document).ready(function() {
    // this part is to expand the child radio selection if particular parent workflow selected
    $("#concernedDirectorChoice").hide();
    $("input[name^=workflowChoice]").change( function (){  
      if($(this).attr("class")=='chooseDir'){
        $("#concernedDirectorChoice").show();
      }else{
        $("#concernedDirectorChoice").hide(); }
      });

    // FORM VALIDATION
    $.validator.addMethod("dirRequired", function(value, element) { 
      return this.optional(element) || ($("input[name^=rdDir:checked]").length);
    }, "That particular workflow requires a Director to be chosen. Please select Director");

    $("#contExpInitiateForm").validate({ 
      debug:true
      ,rules:{ 
        RenewalNo: {required: true, number: true},
        chooseDir: {dirRequired: true},
        workflowChoice: {required: true} }
      ,errorPlacement: function(error, element) {
        $('.errorMessageBox').text(error.html()); }
    });
  });

HTML form part:

   <!-- Pick type of workflow -->
   <table class="hr-table" >
       <tr>  <td class="hr-table-label " colspan=2 >Pick Workflow Type</td> </tr>
       <tr>
         <td> <input type="radio" name="workflowChoice" value="1"> </input> </td>
         <td> Workflow 1 </td>
       </tr>
       <tr>
         <td> <input type="radio" name="workflowChoice" value="2"  class="chooseDir"> </input> </td>
         <td> Workflow 2 (Dir selection required) </td>
       </tr>
       <tr>
         <td> <input type="radio" name="workflowChoice" value="3"> </input> </td>
         <td> Workflow 3 </td>
       </tr>
   </table>    

  <!-- Pick Director for Workflow type 2 -->
     <table id="concernedDirectorChoice" name="concernedDirectorChoice" >     
      <tr><td class="hr-table-label" colspan=2 > Choose Concerned Director</td></tr>
  <tr>
    <td><input type="radio" value='Dir1' name="rdDir" /></td>
    <td>Director 1</td>
  </tr>
  <tr>
    <td><input type="radio" value='Dir2' name开发者_开发百科="rdDir" /></td>
    <td>Director 2</td>
  </tr>
  <tr>
    <td><input type="radio" value='Dir3' name="rdDir" /></td>
    <td>Director 3</td>
  </tr>
   </table>  


Your main issue is probably the selector, the :checked needs to beside the attirubte ([]) selector, so this input[name^=rdDir:checked] should be input[name^=rdDir]:checked.

From your markup, it looks like (to me!) that you want it required if any of <input type="radio" class="chooseDir" /> are selected, so need to change that to .chooseDir:checked anyway.

Also, the rules pairs should be nameOfElement: { rules}, so instead of chooseDir, you want rdDir to actually be required.

Just as a tip though, you can also do this without a custom method (if you're not using it in multiple places, in which case I'd stick with the custom method) since required: can take a selector instead of just a true/false. If the selector finds anything, it's required, if not, it's not required.

Here's everything above put together so you can see that the whole picture looks like:

$("#contExpInitiateForm").validate({ 
  debug:true
  ,rules:{ 
    RenewalNo: {required: true, number: true},
    rdDir: {required: ".chooseDir:checked"},
    workflowChoice: {required: true} }
  ,messages:{
    rdDir: "That particular workflow requires a Director to be chosen. Please select Director" }
  ,errorPlacement: function(error, element) {
    $('.errorMessageBox').text(error.html()); }
});

In the above code, the rdDir radio button list is required if any of the class="chooseDir" radio buttons are selected, and will display the message provided if it's required and is not filled in.

0

精彩评论

暂无评论...
验证码 换一张
取 消