views:

194

answers:

1

I have a asp.net web content from that have a asp.net textbox and I want to use Plugin/Validation but it is not working with me here is my code:

<%@ Page Title="" Language="C#" MasterPageFile="~/Master.Master" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="IMAM_APPLICATION.WebForm1" %>
<%@ Register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" tagprefix="asp" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">


    <script src="js/jquery-1.4.1.js" type="text/javascript"></script>

    <script src="js/jquery.validate.js" type="text/javascript"></script>


    <script type="text/javascript">

        $(document).ready(function() {
        $.validator.addMethod("#<%=TextBox1.ClientID %>", function(value, element) {
            return this.optional(element) || /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,16}$/i.test(value);
        }, "Passwords are 8-16 characters with uppercase letters, lowercase letters and at least one number.");
});

    </script>

</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="ContentPlaceHolder2" runat="server">

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



</asp:Content>
A: 

Well I am unsure what the problem is as this does not tell me much "not working".

Just looking at your code I am trying to see might cause it not to work.

First you don't have the validate method hooked up to any form. I don't think you can run it without hooking it up to a form tag.

// from the validation documentation
$("#commentForm").validate();

Notice how they tie the validate field.

Next

I think your doing your .addMethod wrong

     $(document).ready(function() {
        $.validator.addMethod("#<%=TextBox1.ClientID %>", function(value, element) {
            return this.optional(element) || /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,16}$/i.test(value);
        }, "Passwords are 8-16 characters with uppercase letters, lowercase letters and at least one number.");
});

You have what is above but if you look at the documentation.

addMethod( name, method, [message] ) 

    jQuery.validator.addMethod("math", function(value, element, params) { 
     return this.optional(element) || value == params[0] + params[1]; 
    }, jQuery.format("Please enter the correct value for {0} + {1}"));

see how the first paramter is "name" not "id". Name is like the name of this new method.

So this addMethod could be called "Test101" that would be the name. Or like in the examle you see they named it "math"

next you would do something like this

$("#commentForm").validate
(
   rules:
        {
           txtBoxIdName:
            {
                 math: true;
            }
        }
);

So it would me something like this. You would put the textboxId in the validate method of the form and then you call your .addMethod inside that id. In my case I used the .addMethod called "math" and I believe you just set it to "true" to make it required and to run.

I have not tested this so I will try to find some examples that actually run.

Edit

Here is what the master page.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;

<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<head runat="server">
    <title></title>
    <asp:ContentPlaceHolder id="head" runat="server">
    </asp:ContentPlaceHolder>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server">

        </asp:ContentPlaceHolder>
    </div>
    </form>
</body>
</html>

see it does have a form id and when you hook this master page with an aspx page you should have this.

    <title>

    </title>
    <script>try {  for(var lastpass_iter=0; lastpass_iter < document.forms.length; lastpass_iter++){    var lastpass_f = document.forms[lastpass_iter];    if(typeof(lastpass_f.lpsubmitorig)=="undefined"){      if (typeof(lastpass_f.submit) == "function") {        lastpass_f.lpsubmitorig = lastpass_f.submit;        lastpass_f.submit = function(){          var form = this;          try {            if (document.documentElement && 'createEvent' in document)            {              var forms = document.getElementsByTagName('form');              for (var i=0 ; i<forms.length ; ++i)                if (forms[i]==form)                {                  var element = document.createElement('lpformsubmitdataelement');                  element.setAttribute('formnum',i);                  element.setAttribute('from','submithook');                  document.documentElement.appendChild(element);                  var evt = document.createEvent('Events');                  evt.initEvent('lpformsubmit',true,false);                  element.dispatchEvent(evt);                  break;                }            }          } catch (e) {}          try {            form.lpsubmitorig();          } catch (e) {}        }      }    }  }} catch (e) {}</script></head><body>



        <form name="aspnetForm" method="post" action="Default2.aspx" id="aspnetForm">


<div>
    <input name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUKMTY1NDU2MTA1MmRkJ6rATKUz8lX/wrHNVcM8o9fwof8=" type="hidden">
    </div>

        <div>



        </div>
        </form>
    </body></html>

So try that if that will be good enough for hooking it up.

chobo2
thank you for your reply.in my case I'm using asp.net textbox and inside web content so in my case I have no form!!any advice??
Eyla
You should have a form. Even if your using a master page. You should have one super form tag. Just check the html code on the page when generated
chobo2