doujiu8178 2010-07-07 05:54
浏览 19
已采纳

jquery验证插件:验证某些条件

I m using jquery validator plugin. i need to validate some field with some condition. how do we apply that?

i have a form where user select a country..then user creates branch on the fly, now i want to validate that if user do not select a county then prompt to select country and if user select a country then prompt to create atleast one branch.

what i do as far

if user create any branch then there is a hidden field of counting branch. but that field is filled after creating branch, before that( on page load ) is has no value so if i validate for that hidden field then it always return true. what should i do

here is code snippet i have used but i know there must be a better way to manipulate all above..please suggest me optimize way to do this

<script type="text/javascript">
jQuery.metadata.setType("attr", "validate");

jQuery('#createForm').submit(function(){
        if(jQuery("#hasbranch").val()== 0 ){
            jQuery(".brancherror").html('Atleast one branch is necessary');
            return false;
        }
    });

var validator=jQuery("#createForm").validate({
               rules: { list of rules},
               messgae : { list of messages },                                
});
</script>

<form name="createForm" id="createForm">
<table>
<tr>
      <td>Select Country :</td>
      <td >
      <select name="country_id" id="country"  class="selectbox" onchange="return showBranch();"     title="Please select country" validate="required:true"  >
    <option value="" >Select Country </option>
    <?php foreach($this->country as $key => $country) { ?>
    <option value="<?php echo $country->id; ?>"<?php if ($country->id == $this->ad_data['country_id']) {?> selected="selected" <?php } ?> > <?php echo $country->name; ?></option> 
    <?php } // end of foreach ?>
      </select>
      </td>
  </tr>
  <tr>
      <td> //Here is link to add branch (via iframe) </td>
  </tr>    
  </table>
   <input type="hidden" value="<?php echo count($_SESSION['branches'])? 1: 0 ;?> name="hasbranch" id="hasbranch" />
   </form>  
  • 写回答

1条回答 默认 最新

  • donglie7778 2010-07-07 05:59
    关注

    You could use depends on rule to apply a conditional validation:

    rules: {
        someField: {
           required: true,
           email: {
               depends: function(element) {
                   // require someField to be a valid email only if the user checked 
                   // to be contacted by email
                   return $("#contactform_email:checked");
               }
           }
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题