2016-03-02 119 views
1

我知道這是一種常見的情況,所以我對它進行了搜索,但對我的情況沒有好運。使用Web表單進行jQuery驗證

我不知道什麼是錯誤的,但在文本框時,點擊我送花兒給人得到

Uncaught TypeError: Cannot read property 'settings' of undefined

這裏是我的代碼(簡化爲清楚起見):

<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server"> 

<div id="MessageForm"> 
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> 

    <asp:Button ID="Button1" runat="server" Text="doPostBack" /> 
</div> 

<script type="text/javascript"> 
    $(document).ready(function() { 
     $('#MessageForm').validate({ 
      rules: { 
       <%= TextBox1.ClientID %>: { 
         required: true, 
         minlength: 6, 
         maxlength: 25 
        } 
       }, 
       messages:{ 
        <%= TextBox1.ClientID %>: { 
         required: "Please enter the address.", 
         minlength: $.validator.format("The address name must be at least {0} characters."), 
         maxlength: $.validator.format("The address name must not exceed {0} characters.") 
        } 
       }, 
       errorClass: "error-label", 
       wrapper: "li", 
       errorLabelContainer: "#ErrorSection" 
      }); 
     }); 
</script> 

我已經嘗試過

  • change <div id="MessageForm"><div id="MessageForm" runat="server">
  • 改變$('#MessageForm').validate$('#MainContent_MessageForm').validate
  • 改變<%= TextBox1.ClientID %>到生成的客戶端ID(MainContent_TextBox1)或所產生的名字(搜索Maincontent $ Contenteplaceholder $等)

使用主站時該問題只uccurs頁

這可能是由於我的引用元素是<div>? 我使用Web窗體,所以我不能使用其他<form>標籤

+0

如上所述[這裏](http://stackoverflow.com/questions/4936221/jquery-validate-plugin-on-div)和[這裏](http://stackoverflow.com/questions/487430/validate-輸入那不是在一個窗體與jQuery的驗證插件),你必須使用一個'表格'的jQuery驗證插件。我還沒有測試,但你嘗試[validator.element()](http://jqueryvalidation.org/Validator.element) –

+0

@SyedAliTaqi謝謝,我可以很容易地將元素更改爲'

',但它不夠。我發現令人難以置信的是,現在人們無法輕易地將jquery驗證整合到webforms上。我只是要使用標準的驗證器,謝謝。請張貼您的答案 –

回答

1

如上所述herehere,你必須使用一個form jQuery的驗證插件。我無法確定哪個插件能滿足您的要求,但您可以看看thisthis。希望能幫助到你。