我有一個表單,其中包含一個名爲「wmi_cell_phone」的TextBox和一個RadioButtonList「wmi_send_sms」。 基本上,我需要創建一個驗證器來檢查所選收音機的值是否爲「Y」。如果是這種情況,那麼它檢查TextBox值是否爲空。如果它是空的,那麼它應該通知用戶輸入一個值。使用CustomValidator檢查asp.net中的RadioButtonList和TextBox值時遇到問題
這裏是我的.aspx代碼:
<asp:TextBox ID="wmi_cell_phone" runat="server" MaxLength="100" Width="200px"></asp:TextBox>
<asp:RadioButtonList ID="wmi_send_sms" RepeatDirection="Horizontal" runat="server" Width="140px" CssClass="radio"></asp:RadioButtonList>
和代碼隱藏(VB):
wmi_send_sms.Items(0).Value = "Y"
wmi_send_sms.Items(1).Value = "N"
我的驗證
<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>
卜它似乎不工作..也許我以錯誤的方式訪問RadioButtonList ..
什麼意思'它似乎沒有work'在你的驗證檢查? 你是否已經調試過js函數?發佈你的驗證器的標記。 – 2010-11-20 18:23:11
我的意思是,它沒有做它應該做的事情......如果用戶選擇值爲「是」的收音機,並將文本框留空,它應該提醒用戶他應該在文本框中輸入一個值..但它什麼也不做 – Joe 2010-11-20 20:00:14
您是否已經調試過if js函數被調用?使用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/ – 2010-11-20 20:23:51