簡體   English   中英

RequiredFieldValidator和regularexpressionvalidator不起作用

[英]RequiredFieldValidator and regularexpressionvalidator don't work

我的“ C#” aspx頁面之一中包含以下代碼。 當我單擊cmdPassword時,即使文本框為空,它也會觸發事件。 為什么兩個驗證都沒有停止它?

<tr>
  <td align="left" valign="top">Password:</td>
  <td align="left" valign="top">
    <asp:TextBox id="txtPassword" ValidationGroup="vgPassword" runat="server" Font-Size="Small" Width="200px" MaxLength=15 textmode="Password" />
            <br /><font class="pagetext_7">(between 6 and 15 characters long)</font>
            <br />
            <asp:literal id="litPassword" runat="server"></asp:literal>
    </td>
    <td valign="top" align="left">
      <asp:Button ID="cmdPassword" CausesValidation="true" runat="server" OnClick="cmdPassword_Click" validationgroup="vgPassword" style="color:#284E98;background-color:White;border-color:#507CD1;border-width:1px;border-style:Solid;font-family:Verdana;font-size:0.8em;" Text="Submit" height="20px" />
    </td>
    <td>
      <asp:RequiredFieldValidator ID="RequiredFieldValidator4" ControlToValidate="txtPassword" runat="server" ErrorMessage="*"  validationgroup="vgPassword" />
      <asp:regularexpressionvalidator id="valPassword" runat="server" validationgroup="vgPassword" errormessage="Password must be between 6 and 15 characters long" validationexpression="^.{6,15}$" controltovalidate="txtPassword"></asp:regularexpressionvalidator>
    </td>
   </tr>

更新:我只是將下面的代碼添加到按鈕單擊事件中。

protected void cmdPassword_Click(object sender, EventArgs e)
{
  if (txtPassword.Text.Length == 0)
  {
    litPassword.Text = "<font color='red'>Password cannot be blank.</font>";
  }
  else
  {
    //Password update code
  }
}

有人用這種方式看到任何問題嗎?

通常regularexpressionvalidator如果文本框是empty.So沒有錯,你的代碼。如果想它也檢查它甚至你的文本框為空,你必須改變你的正則表達式沒有被解雇。

這就是我最終做的方式

protected void cmdPassword_Click(object sender, EventArgs e)
{
   if (txtPassword.Text.Length == 0)
   {
     litPassword.Text = "<font color='red'>Password cannot be blank.</font>";
   }
   else
   {
    //Password update code
   }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM