开发者

Having trouble with CustomValidator that checks RadioButtonList and TextBox values in asp.net

I have a form with contains, among others, a TextBox called "wmi_cell_phone" and a RadioButtonList "wmi_send_sms". Basically, I need to create a validator that checks whether the value of the selected radio is "Y". If this is the case, then it checks if the TextBox value is empty or not. If it is empty, then it should notify the user to enter a value.

Here is my .aspx code:

<asp:TextBox ID="wmi_cell_phone" ru开发者_C百科nat="server" MaxLength="100" Width="200px"></asp:TextBox>
<asp:RadioButtonList ID="wmi_send_sms" RepeatDirection="Horizontal" runat="server" Width="140px" CssClass="radio"></asp:RadioButtonList>

and code-behind (VB):

wmi_send_sms.Items(0).Value = "Y"
wmi_send_sms.Items(1).Value = "N"

My validator

<asp:CustomValidator ID="val_wmi_send_sms" runat="server"
ClientValidationFunction="ValidateSMS"
Display= "Dynamic"
ErrorMessage="Please enter a valid phone number."> </asp:CustomValidator>

<script language="javascript" type="text/javascript">                  
function ValidateSMS(Source, args) 
{

  var smsRadio = document.getElementsByName('<%= wmi_send_sms.ClientID %>');
      var cellphone = document.getElementById('<%= wmi_cell_phone.ClientID %>');    

   for (var x = 0; x < smsRadio.length; x ++) 
   {
      if (smsRadio[x].checked) 
      {
          if (smsRadio[x].value == "Y") 
          {
              if (cellphone.value == "")
                   args.IsValid = false;
              else
                   args.IsValid = true;
          }
      }
    }
}
</script>

Bu it doesn't seem to work.. Maybe I'm accessing the RadioButtonList in the wrong way..


You should first debug if the ClientValidationFunction gets called by the Validator.

  • Use developer tools for ie: http://www.microsoft.com/downloads/en/details.aspx?FamilyID=95e06cbe-4940-4218-b75d-b8856fced535
  • FireBug for Firefox: https://addons.mozilla.org/en-US/firefox/addon/1843/

If the Validator should work even if nothing was selected you have to set ValidateEmptyText to true.

  • Does the ValidationGroup value match in all 3 places: the submit button, the validator control, and the control to validate?
  • Does the submit button have CausesValidation=true?
  • Does the RadioButtonList has CausesValidation=true?


"Maybe I'm accessing the RadioButtonList in the wrong way.."

Yep.

Look at the markup generated, it will be something like this:

<table id="wmi_send_sms" class="radio" style="width:140px;">
  <tr>
    <td>
      <input id="wmi_send_sms_0" type="radio" name="wmi_send_sms" value="Y" />
      <label for="wmi_send_sms_0">Y</label>
    </td>
    <td>
      <input id="wmi_send_sms_1" type="radio" name="wmi_send_sms" value="N" />
      <label for="wmi_send_sms_1">N</label>
    </td>
  </tr>
</table>

Your <%= wmi_send_sms.ClientID %> tag is getting the ID of the table enclosing the radio button list.

Personally, when working on the client side, I find it much easier to avoid RadioButtonList. Just create two RadioButton controls, using the GroupName property to associate them with each other.

<asp:RadioButton ID="wmi_send_sms_y" runat="server" GroupName="wmi_send_sms" Text="Y" />
<asp:RadioButton ID="wmi_send_sms_n" runat="server" GroupName="wmi_send_sms" Text="N" />

Then you can check if the "Y" one is checked in your validator by accessing <%= wmi_send_sms_y.ClientID %>

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜