JQuery validation Engine not working in ASP Page With Master File

心已入冬 提交于 2019-12-25 18:02:58

问题


I downloaded this jquery validation engine from http://www.position-absolute.com/articles/jquery-form-validator-because-form-validation-is-a-mess/

All validation is Working fine except Confirm Password validation it validates all the time wheather password is matching or not.

validation works perfecrt on individual page but its not working in content page of the master page.

please help me...

.aspx file

<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<script src="Styles/jquery-1.6.min.js" type="text/javascript"></script>
    <script src="Styles/jquery.validationEngine-en.js" type="text/javascript"></script>
    <script src="Styles/jquery.validationEngine.js" type="text/javascript"></script>
    <link href="Styles/validationEngine.jquery.css" rel="stylesheet" type="text/css" />
     <script type="text/javascript">
         jQuery(document).ready(function () {
             // binds form submission and fields to the validation engine
             jQuery("#form1").validationEngine();
         });
     </script>
     <form id="form1" runat="server">
     <br />
     <br />
     <br />
     <asp:TextBox ID="TextBox1" runat="server" class="validate[required] text-input"></asp:TextBox>
    <br /><asp:TextBox ID="TextBox2" runat="server" class="validate[required,equals[TextBox1]] text-input"></asp:TextBox>
    <br />
    <asp:Button ID="Button1" runat="server" Text="Button" />

      </form>
</asp:Content>

回答1:


Ok, so you have a couple of things here. First off, you should probably not put form element in content pages. Typical ASP.NET the form is in master page. You might be making more work for yourself but.. I can understand why you might do this. So your main problem is that when you have content in master page, (the default behavior) the ID fields will get prefixed so that they do not collide with ids from content in another placeholder.

If you view source, "TextBox1" will look something like

<input name="ctl00$MainContent$TextBox1" type="text" value="asdf" id="MainContent_TextBox1" class="validate[required] text-input" />

Your validate is looking for TextBox1 not MainContent_TextBox1.

You have a couple of options. It's important to understand ramifications of each (I'll leave that for you to research).

Option 1: At the top of your master page (in its declaration) you can put this in your ClientIDMode="Static". ASP.NET will not touch your IDs, if they collide, it's your responsibility.

Option 2: I don't care for this but have used it on occasion. Get the client ID from asp.net

validate[required,equals[<%= TextBox1.ClientID %>]]

Option 3: There may be other possibility of putting in a jquery selector that finds id like TextBox1

hope this helps




回答2:


When .net renders it changes the Form id so instead of using:

jQuery("#form1").validationEngine(); 

Please use:

jQuery("form").validationEngine();

for view source and get the id of your form and place it there




回答3:


I got same problem with you and solved this problem.

You can do it easily.

ADD THIS TO TOP OF MASTER PAGE (I only added ClientIDMode="Static")

<%@ Master Language="C#" AutoEventWireup="true" CodeFile="geneticAjans.master.cs" Inherits="geneticAjans_gen" ClientIDMode="Static" %>

ADD THIS TO INSIDE OF $(document).ready(); (You should write your master page form id)

$("#Button1").click(function(){
    if ($("#form1").validationEngine('validate') == false)
        return false;
});

Now, your JQuery Validation Engine should be work.



来源:https://stackoverflow.com/questions/7269557/jquery-validation-engine-not-working-in-asp-page-with-master-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!