控制項驗證

摘要:控制項驗證

<asp:TextBox ID="txtProceed" runat="server" TextMode="MultiLine" Rows="2" Columns="60" />

//----------------- 限制控制項不能為空白 -------------------------------------------------------------------
<asp:RequiredFieldValidator ID="rfvProceed" runat="server" ControlToValidate="txtProceed" Display="Dynamic">
              <div>「進行方式」不可空白!</div>
</asp:RequiredFieldValidator>

//----------------- 限制控制項輸入的數值範圍 -------------------------------------------------------------------
    <asp:RangeValidator ID="RangeValidator1" runat="server" ControlToValidate="txtTenValue" Display="None"  ErrorMessage="數值不可超過2位數"   MaximumValue="88"  MinimumValue="1" ></asp:RangeValidator>

//----------------- 限制控制項輸入的長度 -------------------------------------------------------------------
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server"
  ErrorMessage="超過限制輸入長度為150字" ControlToValidate="txtQuest1" ValidationExpression="[\s\S]{0,150}">
</asp:RegularExpressionValidator>


//---------------------- 驗 證 電 子 信 箱 格 式   ---------------------------------------------------------
<asp:RegularExpressionValidator ID="revEmail" runat="server" ControlToValidate="txtEmail" Display="Dynamic"
  ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*">
<span>「電子信箱」格式有誤!</span>
</asp:RegularExpressionValidator>


//------------------- 判別輸入的格式     --------------------------------------------
  <asp:RegularExpressionValidator ID="revAccount" runat="server" ControlToValidate="txtAccount"  Display="Dynamic"
                                ValidationExpression="^[a-zA-Z]{1}[a-zA-Z0-9]{3,19}$">
    <span>「帳號」格式有誤!</span>
  </asp:RegularExpressionValidator>

//  8~20個英文或數字的組合(區分大小寫)
//  需包含一個英文字及一個數字的組合(第一碼必需是英文字)

 

//-----------------警示輸入為大小寫-----------------------------------------------

  <script type="text/javascript">
        function KeyPress(objTR) {//輸入為大寫提醒功能
            var txtval = objTR.value;
            var key = event.keyCode;
            if (key >= 65 && key <= 90)
                document.getElementById('capslock').style.display = '';
            else
                document.getElementById('capslock').style.display = 'None';
        }

        function CloseDiv() {
            document.getElementById('capslock').style.display = 'None';
        }

    </script>

  <asp:TextBox ID="txtPwd" runat="server" TextMode="Password" MaxLength="20" Columns="21" onkeypress="javascript:KeyPress(this);"
                        onkeydown="javascript:CloseDiv();"></asp:TextBox

 

//----------------- 判別值是否相同,通常用來再次確認輸入的密碼 ------------------------

 <asp:CompareValidator ID="cvCheck" runat="server" ControlToValidate="txtCheck" ControlToCompare="txtPasswd" Display="Dynamic">
     <span>密碼與確認密碼兩者不吻合!</span>
  </asp:CompareValidator>


------------------判斷下拉式選單是否已選擇----------------------------------------------------------------------
<asp:RequiredFieldValidator id="reqFavoriteColor" Text="請選擇國家" InitialValue=""
ControlToValidate="dropContry" Runat="server" Display="Dynamic"/>

 

-------------------收集驗證訊息-------------------------------------------------------------------

<asp:ValidationSummary ID="ValidationSummary1" runat="server"  DisplayMode="List" />