2015-05-23 82 views
1

我試圖用asp.net/bootstrap做一個驗證表單。我正在用一個文本框測試它。使用引導驗證asp.net

目前,這是我的代碼:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> 

<!DOCTYPE html> 

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
    <title></title> 
    <link href="Content/bootstrap.min.css" rel="stylesheet" /> 
    <script src="Scripts/jquery-1.10.2.min.js"></script> 

</head> 

    <body> 

    <form id="form1" runat="server"> 
     <asp:TextBox runat="server" CssClass="form-control" ID="userNameTextBox" /> 
     <input id="Submit1" type="submit" value="submit" /> 
    </form> 

    <script type="text/javascript"> 
     $(document).ready(function() { 
      $('#form1').formValidation({ 
       framework: 'bootstrap', 
       icon: { 
        valid: 'glyphicon glyphicon-ok', 
        invalid: 'glyphicon glyphicon-remove', 
        validating: 'glyphicon glyphicon-refresh' 
       }, 
       fields: { 
        <%=userNameTextBox.UniqueID%>: { 
        validators: { 
         notEmpty: { 
          message: 'The username is required and cannot be empty' 
         }, 
         stringLength: { 
          min: 6, 
          max: 30, 
          message: 'The username must be more than 6 and less than 30 characters long' 
         }, 
         regexp: { 
          regexp: /^[a-zA-Z0-9]+$/, 
          message: 'The username can only consist of alphabetical and number' 
         }, 
         different: { 
          field: 'password', 
          message: 'The username and password cannot be the same as each other' 
         } 
        } 
       } 
      } 
     }); 
    }); 
    </script> 
</body> 
</html> 

但是,當我按一下按鈕沒有任何反應。我究竟做錯了什麼??感謝您的幫助!

+0

有人請嗎? – Jenssen

+0

我想你可以像這樣做驗證http://1000hz.github.io/bootstrap-validator/ – sheshadri

回答

0

我知道這有點舊,但是您希望使用<%=userNameTextBox.ClientID%>而不是<%=userNameTextBox.UniqueID%>來獲取要在JavaScript中引用的HTML元素的生成ID。有關不同ASP.NET id值的更多詳細信息,請參閱this answer