2015-12-22 69 views
1

我有一個asp.net web應用程序,我希望使用表單驗證io jquery插件來執行客戶端驗證。ASP.Net Webforms和驗證

我有表單設置和插件驗證字段,因爲它們被更改,但是如果其中一個字段被驗證,它允許提交表單的其餘部分,即使我有空的必填字段。

asp.net Web窗體頁:

<%@ Page Title="" Language="VB" MasterPageFile="~/MasterPage.master" AutoEventWireup="false" CodeFile="NewUser.aspx.vb" Inherits="NewUser" ClientIDMode="Static" %> 

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server"> 
<link href="Layout/formvalidation/css/formValidation.min.css" rel="stylesheet" /> 
</asp:Content> 
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server"> 
<div class="row" id="NewUserForm" role="form"> 
    <h1 class="col-xs-12">New User Form:</h1> 
    <div class="form-group"> 
     <label class="control-label col-sm-2" for="<%= UsernameText.UniqueID %>">Username:</label> 
     <div class="col-sm-10"> 
      <asp:TextBox runat="server" ID="UsernameText" CssClass="form-control" placeholder="Username" /> 
     </div> 
    </div> 
    <div class="form-group"> 
     <label class="control-label col-sm-2" for="<%= EmailText.UniqueID %>">Email:</label> 
     <div class="col-sm-10"> 
      <asp:TextBox runat="server" ID="EmailText" CssClass="form-control" TextMode="Email" Type="email" placeholder="Ëmail address"/> 
     </div> 
    </div> 

    <div class="form-group"> 
     <label class="control-label col-sm-2" for="<%= PasswordText.UniqueID %>">Password:</label> 
     <div class="col-sm-10"> 
      <asp:TextBox runat="server" ID="PasswordText" CssClass="form-control" TextMode="Password" placeholder="Password" /> 
     </div> 
    </div> 

    <div class="form-group"> 
     <label class="control-label col-sm-2" for="<%= ConfirmPasswordText.UniqueID %>">Confirm Password:</label> 
     <div class="col-sm-10"> 
      <asp:TextBox runat="server" ID="ConfirmPasswordText" CssClass="form-control" TextMode="Password" placeholder="Confirm password" /> 
     </div> 
    </div> 
    <asp:LinkButton ID="LinkButton1" runat="server" Text="Button" CssClass="btn btn-primary disabled" type="submit" /> 
</div> 
<br /> 
<div class="row"> 
    <div class="col-xs-12"> 
     <asp:LinkButton runat="server" ID="CreateUserButton" text="Create User" CssClass="btn btn-info" /> 
    </div> 
</div> 


<script src="Layout/formvalidation/js/formValidation.min.js"></script> 
<script src="Layout/formvalidation/js/framework/bootstrap.min.js"></script> 
<script type="text/javascript"> 
    $(document).ready(function() { 
     $('#NewUserForm').formValidation({ 
      framework: 'bootstrap', 
      icon: { 
       valid: 'glyphicon glyphicon-ok', 
       invalid: 'glyphicon glyphicon-remove', 
       validating: 'glyphicon glyphicon-refresh' 
      }, 
      fields: { 
       // There is no single quote 
       <%=UsernameText.UniqueID%>: { 
        validators: { 
         notEmpty: { 
          message: 'The username is required and cannot be empty' 
         }, 
         different: { 
          field: '<%=PasswordText.uniqueID%>', 
          message: 'The username and password cannot be the same as each other' 
         } 
        } 
       }, 
       <%=EmailText.UniqueID%>: { 
        validators:{ 
         notEmpty: { 
          Message: 'The email field is required and cannot be empty' 
         }, 

         email: { 
          Message: 'Please enter a valid email address!' 
         } 
        } 
       }, 

       <%=PasswordText.uniqueID%>: { 
        validators:{ 
         notEmpty: { 
          Message: 'A password is required and cannot be empty' 
         }, 
         stringLength: { 
          min: 7, 
          max: 30, 
          message: 'The password must be more than 7 and less than 30 characters long' 
         }, 
         regexp: { 
          regexp: /^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[#[email protected]$%^&*-]).{8,}$/,         
          message: 'Password must contain at least 1 uppercase, 1 lowercase, 1 number and 1 special character' 
         }, 
         identical: { 
         field: '<%=ConfirmPasswordText.UniqueID%>', 
         message: 'Passwords do not match' 
        } 
       } 
      }, 

      <%= ConfirmPasswordText.UniqueID%>:{ 
       validators:{ 
        identical: { 
         field: '<%=PasswordText.UniqueID%>', 
         message: 'Passwords do not match' 
        } 
       } 
      } 
     } 
    }); 
}); 
</script> 

任何建議,我怎麼能得到的頁面之前驗證所有控件提交 - 如果有任何驗證失敗,以防止形式從提交。

插件即時通訊使用的是:http://formvalidation.io/

回答

0

OnClientClick property是什麼您這裏需要。將它添加到您的提交按鈕並指定一個Javascript函數,它將執行您的驗證表單。如果此函數返回true,表單將正常提交,如果false,提交將被阻止。

在你的情況與formvalidation.io(from the API docs),你會做這樣的事情:

<asp:LinkButton runat="server" ID="CreateUserButton" text="Create User" CssClass="btn btn-info" OnClientClick="return isValidClient()" /> 

... 
<script type="text/javascript"> 
    function isValidClient() { 
     return $('#NewUserForm').data('formValidation').validate().isValid(); 
    } 
</script> 

這裏最重要的是要確保你的功能和屬性的OnClientClick都有return - 否則無法工作!