Click here to Skip to main content
14,357,121 members
Rate this:
Please Sign up or sign in to vote.
Im trying to validate mutiple asp textboxes in visual studio. I want to use jquery but i really can't seem to implement it. I keep getting errors relating to

Unable to get value of the property 'form': object is null or undefined

Can someone please give me explicit implementation instructions / code to get it working. The examples ive been trying are usint the Validator.addmethod() as im trying to ad regex validation.

Here is my effort so far


var alNumRegex = /^[\d\d?]+$/;    if (alNumRegex.test($('txtStones').val()))    { alert("Invalid Input"); }


Does Nothing

$.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").rules("add", { regex: "^[\d\d?]+" })


Gives error - Microsoft JScript runtime error: Unable to get value of the property 'form': object is null or undefined
And points to this code in jquery.validate.js

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;

This line in particular

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


Here is the full html page im using. At the moment im just trying to validate one textbox to get it working then i will be validating a further 5.

<<pre lang="xml">%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head runat="server">
    <script src="scripts/jquery-1.5.2.js" type="text/javascript"></script>
    <script src="scripts/jquery.validate.js" type="text/javascript"></script>
    <script type="text/javascript">
    //<![CDATA[
    $.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").rules("add", { regex: "^[\d\d?]+" })
    //]]>
    </script>
    <script type="text/javascript">
</script>
    <title></title>
    <style type="text/css">
/*<![CDATA[*/
        .style1        {            width: 79%;        }        .style2        {            width: 142px;        }
    /*]]>*/
    </style>
  </head>
  <body>
    <form id="form1" runat="server">
      <div>
        <table class="style1">
          <tr><td class="style2"></td><td>.</td></tr>
          <tr><td class="style2"></td><td></td></tr>
          <tr><td class="style2"></td><td></td></tr>
          <tr><td class="style2"></td><td>Male Female</td></tr>
          <tr><td class="style2"></td><td></td></tr>
          <tr><td class="style2">&amp;nbsp;</td><td></td></tr>
          <tr><td class="style2"></td><td></td></tr>
          <tr><td class="style2"></td><td></td></tr>
          <tr><td class="style2"></td><td></td></tr>
          <tr><td class="style2"></td><td></td></tr>
          <tr><td class="style2"></td><td></td></tr>
          <tr><td class="style2"></td><td></td></tr>
          <tr><td class="style2"></td><td></td></tr>
        </table>.
      </div>
    </form>
  </body>
</html>
Posted
Updated 4-May-11 9:54am
v2

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

The first and most obvious thing is that your form in the page is called "form1" and not "form", so it make complete sense that the javascript cannot find it.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100