Microsoft JScript runtime error: 'form.elements' is null or not an object runtime error while

I have an asp page which submits the form to another page. I call a javacript function to check the fields before submitting.

I get the following error in IE when i submit the form.
The date field is a textbox which takes input from a calender control.
 Code is below. please suggest.


Thanks
function checkForm(elem)
{
	if(elem.form.elements['pubdate'].value =='' )
	{
		alert('Please specify the end date');
		elem.form.elements['pubdate'].focus();
		return false;
	}

return true;
}


----------------------------

this is my form post..
<form method="post" action="MAction.asp" onsubmit="return checkForm(this);">

i have a save button which has an onclick check as below, though i removed the onclick parameters the error occurs.

<input type="submit" value="Save" onclick="return checkForm(this);" name="Submit1">

Open in new window

nirisanAsked:
Who is Participating?
 
kadabaCommented:
It will work if you have a pattern similar to the attached code...
If its a server side control the client side id name might differ than whats provided.

hence you might be getting the error.
paste the html code from the browser.

<html>
<head>
<title>form validation</title>
<script type="text/javascript">
function checkForm(elem)
{
	if(elem.elements['pubdate'].value =='' )
	{
		alert('Please specify the end date');
		elem.elements['pubdate'].focus();
		return false;
	}
	return true;
}

</script>
</head>
<body>
<form action="http://www.google.com" onsubmit="return checkForm(this);">
	Date :<input type="text" id="pubdate" name="pubdate">
	<br>
	<input type="submit" name="mySubmit" value="Submit">
</form>
</body>
</html>

Open in new window

0
 
leakim971PluritechnicianCommented:
Hello nirisan,

Assuming you pass the form itself as object to checkForm you don't need to add form again :


function checkForm(elem)
{
        if(elem.elements['pubdate'].value =='' )
        {
                alert('Please specify the end date');
                elem.elements['pubdate'].focus();
                return false;
        }

return true;
}


----------------------------

this is my form post..
<form method="post" action="MAction.asp" onsubmit="return checkForm(this);">

i have a save button which has an onclick check as below, though i removed the onclick parameters the error occurs.

<input type="submit" value="Save" onclick="return checkForm(this);" name="Submit1">

Open in new window

0
 
nirisanAuthor Commented:
Hi leakim971,

I have removed the form as you suggested. But i still get this error. Please advise.

Microsoft JScript runtime error: 'elements.pubdate' is null or not an object
0
 
leakim971PluritechnicianCommented:
Could you post your entire page ?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.