Click to See Complete Forum and Search --> : getting "null or not an object" error


IndyB
06-13-2003, 02:06 PM
I've got a validation script that's giving me an error.

The error is: 'document.forms[...].elements' is null or not an object

Can anyone see what's going wrong here?

Here's the javascript function:
function docard_expiration(str,formname,fieldname)
{
reg = /\d+\W\d\d\d\d/;
newstr = str.replace(reg, "");
if (newstr != "")
{
document.forms[formname].elements[fieldname].value = "MM/YYYY";
}
else
{
reg = /(\d+)\W(\d\d\d\d)/;
month = str.replace(reg, "$1");
year = str.replace(reg, "$2");
checkmonth = Number(month) + 1;
tmpdate = new Date(checkmonth + "/1/" + year);
tmpdate = tmpdate - 86400000;
now = new Date();
timeDifference = now - tmpdate;
if (timeDifference < 0)
{
doformatdate(tmpdate);
document.forms[formname].elements[fieldname].value = formatdate;
}
else
{
if (document.forms[formname].elements[fieldname].value != "")
{
doformatdate(tmpdate);
alert("Expiration Date " + formatdate + " invalid.");
}
document.forms[formname].elements[fieldname].value = "MM/YYYY";
}
}
}

Here's the form field associated w/ this function:
<input type="Text" name="ExpDateCC" size="25" maxlength="17" onChange="docard_expiration('this.value', 'this.form.name', 'this.name')" onBlur="docard_expiration(this.value, this.form.name, this.name)">

Jona
06-13-2003, 02:30 PM
Try using eval():


eval("document.forms["+formname+"].elements["+fieldname+"].value = 'MM/YYYY'");


Jona

IndyB
06-13-2003, 02:45 PM
Wow Jona, you're on it today! Thanks again for helping out. I changed all of those statements to evals, but I'm still getting the same error. Did you mean I should change them all, or just a specific one?

Khalid Ali
06-13-2003, 02:55 PM
since you did not post your code here so I am guessing..
You must have not have set the form elements name attribute...thats the one I can nt see ..you do have the name for input set..

other then there is nothing wrong with your code

IndyB
06-13-2003, 03:02 PM
The form name attribute is defined

<form name="frmpage" ....

Khalid Ali
06-13-2003, 03:15 PM
Here is the code It seem to work for me


<script type="text/javascript">
<!--

function docard_expiration(str,formname,fieldname)
{
reg = /\d+\W\d\d\d\d/;
newstr = str.replace(reg, "");
if (newstr != "")
{
document.forms[formname].elements[fieldname].value = "MM/YYYY";
}
else
{
reg = /(\d+)\W(\d\d\d\d)/;
month = str.replace(reg, "$1");
year = str.replace(reg, "$2");
checkmonth = Number(month) + 1;
tmpdate = new Date(checkmonth + "/1/" + year);
tmpdate = tmpdate - 86400000;
now = new Date();
timeDifference = now - tmpdate;
if (timeDifference < 0)
{
doformatdate(tmpdate);
document.forms[formname].elements[fieldname].value = formatdate;
}
else
{
if (document.forms[formname].elements[fieldname].value != "")
{
doformatdate(tmpdate);
alert("Expiration Date " + formatdate + " invalid.");
}
document.forms[formname].elements[fieldname].value = "MM/YYYY";
}
}
}
</script>
</head>
<body>

<form id="form1" name="form1" action="" onsubmit="">
<input type="Text" name="ExpDateCC" size="25" maxlength="17"
onchange="docard_expiration('this.value', 'this.form.name', 'this.name')"
onblur="docard_expiration(this.value, this.form.name, this.name)">
<input type="text" name="t1"/>
<input type="button" value="process" onclick="openpopup()"/>
</form>
</body>
</html>

IndyB
06-13-2003, 03:39 PM
Still not workig for me. Same error.

I noticed that when I remove the " ' " from my form variables on the onChange and onBlur methods, I no longer get an error. But, no matter what I enter into the field, it gets replaced with " MM/YYYY "

What's going on here?

Khalid Ali
06-13-2003, 03:59 PM
What are you trying to do here?

Help me understand it.

1.if the date enterd by user is wrong format show an error and populate the text field with the correct format.

2.If the date is of correct format make sure the expiration date is not before todays date.If its show error else....do some else???/
am I right?

Jona
06-13-2003, 03:59 PM
Yes, there should be no apostrophes in the event handler. Also, the code you made works.. The way it's set up is to make it do that. lol

Jona

IndyB
06-13-2003, 03:59 PM
If I take out the onChange method, and just do onBlur with no single quotes around the variables, it works fine. I wanted to use both onBlur and onChange but maybe that's not possible??? Strange...

It works properly, that is, it doesn't change the input back to MM/YYYY unless it's incorrect.

Jona
06-13-2003, 04:40 PM
It should work without the apostrophes and both event handlers.

Jona