开发者

Required field validator issue for multiple controls

In my aspx page in a submission form i have following html.

<p><asp:CheckBox ID="chkWishToDonateFrmTrust " runat="server" onclick="chkWishToDonateFrmTrustHandle(th开发者_如何学JAVAis)"  />
Wish to donate from following Trust for future transactions</p><p>&nbsp;</p><p><textarea name="textarea" cols="45" rows="5" class="txtTrustDetails" runat="server"
                                            id="txtTrustDetails" ></textarea></p>

I need to have a required field validation control to validate txtTrustDetails text area only if chkWishToDonateFrmTrust is checked by the user without server post backs for this.only javascript library I am using is Microsoft Ajax Framework.I also have to include this to validation group in form with some other for controls.I already know as my knowledge one required field validator control can only validate single UI control(asp.net forum thread) does anyone in community dealt with this kind of issue in intuitive way?


As another answer has said, use a Custom Validator and do something like this (not tested so may not be quite right...):

  <script type="text/javascript">
    function validate(sender, args) {
        var checkBox = document.getElementById('<%=CheckBox1.ClientID %>');
        var textBox = document.getElementById('<%=TextBox1.ClientID %>');

        if (checkBox.checked == 1) {

            if (textBox.value == '') {
                args.IsValid = false;
            } else { args.IsValid = true; }
        } 
    }

</script>

 <asp:CheckBox ID="CheckBox1" runat="server" />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:CustomValidator ID="CustomValidator1" ControlToValidate="TextBox1" runat="server"
    ErrorMessage="CustomValidator" ClientValidationFunction="validate"></asp:CustomValidator>

Use the custom validator and use the clientValidationfunction to call a javascript function to check your values. You should also have a servervalidate function incase javascript is off.


you can use a js function or a jquery function to do this is a sample code of how to achieve this

function chkValidate()
  {
     if($("#chkWishToDonateFrmTrust ").checked)
      {
          if($("#txtTrustDetails").val()=='')
            {
              args.IsValid = false;
              //your custom message according to you
             }

       }

}

call this function on your sumbit button onclick;

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜