开发者

How do I utilise addmethod() in the jquery validation plugin

开发者 https://www.devze.com 2023-03-03 04:49 出处:网络
well, i\'ve been at it for 5 days and got 0, nill, zippo and nada code added to my uni project!! (due on the 9th :/)

well, i've been at it for 5 days and got 0, nill, zippo and nada code added to my uni project!! (due on the 9th :/)

PLEASE can someone give me FULL utilization code for what I have below? Remember im a complete noob so please include the OBVIOUSE stuff that usually gets left out.

(To assuage any moral problems with giving code for uni projects were only getting marked on knowing what to utilise, not how so the coding is immaterial.)

Heres what I have (just a test page to prove to myself it wasn't anything freaky to do with the rest of my code)

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

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

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <script开发者_开发问答 src="jquery-1.4.2.js" type="text/javascript"></script>
    <script src="jquery.validate.js" type="text/javascript"></script>
    <script language="javascript" type="text/javascript">


            $.validator.addMethod(
    "regex",
     function (value, element, regexp) {
         var check = false;
         var re = new RegExp(regexp);
         return this.optional(element) || re.test(value);
     },
        "Please check your input.");

        $('#<%= txtStones.ClientID %>').rules("add", { regex: "/\d\d?/" })

        </script>
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="txtStones" runat="server"></asp:TextBox>
        <asp:Button ID="Button1" runat="server" Text="Button" />
    </div>
    </form>
</body>
</html>

Thats it, my entire code...and this is what i get

Microsoft JScript runtime error: Unable to get value of the property 'form': object is null or undefined

And it points to this line in the jquery.validate.js file

var settings = $.data(element.form, 'validator').settings;

rules: function(command, argument) {
        var element = this[0];

        if (command) {
            var settings = $.data(element.form, 'validator').settings;
            var staticRules = settings.rules;
            var existingRules = $.validator.staticRules(element);
            switch(command) {
            case "add":
                $.extend(existingRules, $.validator.normalizeRule(argument));
                staticRules[element.name] = existingRules;
                if (argument.messages)
                    settings.messages[element.name] = $.extend( settings.messages[element.name], argument.messages );
                break;
            case "remove":
                if (!argument) {
                    delete staticRules[element.name];
                    return existingRules;
                }
                var filtered = {};
                $.each(argument.split(/\s/), function(index, method) {
                    filtered[method] = existingRules[method];
                    delete existingRules[method];
                });
                return filtered;
            }
        }

Thats everything. Iknow its probably a total donkey idiotic thing thats not being done by me, but i do still really really need to get this working.

I really really appreciate your input.

EDIT

Well im out of time so im going to have to do what I REALLY didn't want to do. Im going to have approximately 200 asp.net validation control tools and god knows how many extenders!!

I would still appreciate your input for furure projects.


The first thing that I notice is that you're attempting to access the form when it hasn't even been loaded yet. You have to put your code in the DOM ready event so that it waits for the whole DOM to be loaded before it starts running. Basically, when your code is run, the <form> doesn't exist yet which is why you're getting that error. Try changing:

$('#<%= txtStones.ClientID %>').rules("add", { regex: "/\d\d?/" })

To:

$(function() {
    //This function will be called only when the DOM is ready. At this point,
    // your <form> will exist.
    $('#<%= txtStones.ClientID %>').rules("add", { regex: "/\d\d?/" })
});

See if that helps.


Edit
Also, according to this, the third parameter for the callback function appears to be an array (see the examples on the linked page). So you may have to change: var re = new RegExp(regexp); to var re = new RegExp(regexp[0]); (to get the first parameter).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号