Please help me how to call this Javascript

S

settyv

Hi,

Below is the Javascript function that am trying to call from asp:Button
control.

<script language="javascript">
function ValidateDate(fromDate,toDate)
{

var fromDate=new Date();
var toDate=new Date();
var diffDate = Math.round( ( toDate-fromDate ) / 864e5 );
alert(diffDate);
if( diffDate < 0 )
{
alert( "The start date cannot be after the end date!")
return false;
}

if( diffDate > 30 )
{
alert( "Please enter dates less than 31 days apart" )
return false;
}

return true; //allow submit
}
</script>


In code-behind am calling the function like as below:
btnSubmit.Attributes.Add("onClick"," return ValidateDate(" +
txtFromDate.Text.ToString() + "," + txtToDate.Text.ToString() + ");");



But if start entering the dates,it submits the page as it takes both
datee as current dates.Please let me know the reason.

Thanks,
Vishnu
 
R

rampabbaraju

Try this

btnSubmit.Attributes.Add("onClick"," return
ValidateDate(document.GetElementById('" +
txtFromDate.ClentId() + ').Value",document.GetElementById('" +
txtToDate.ClentId()+ "'));");
 
S

settyv

Hi,

It is not working .it is telling script error.Here is the HTML created
after the postback.Everything is fine.But i dont know why it is telling
error.

Microsoft JScript runtime error: 'document.GetElementById.txtFromDate'
is null or not an object

<TR vAlign="top">
<TD colSpan="4" height="25"></TD>
<TD><input type="submit" name="btnSubmit" value="Submit"
id="btnSubmit" onClick="return
ValidateDate(document.getElementById["txtFromDate"].Value,document.getElementById["txtToDate"].Value);"
/></TD>
</TR>

Please let me know how can i solve this.


Thanks,
Vishnu
 
G

Guest

Show us more code (espacially text boxes declaration)
--
Milosz Skalecki
MCP, MCAD


Hi,

It is not working .it is telling script error.Here is the HTML created
after the postback.Everything is fine.But i dont know why it is telling
error.

Microsoft JScript runtime error: 'document.GetElementById.txtFromDate'
is null or not an object

<TR vAlign="top">
<TD colSpan="4" height="25"></TD>
<TD><input type="submit" name="btnSubmit" value="Submit"
id="btnSubmit" onClick="return
ValidateDate(document.getElementById["txtFromDate"].Value,document.getElementById["txtToDate"].Value);"
/></TD>
</TR>

Please let me know how can i solve this.


Thanks,
Vishnu

Try this

btnSubmit.Attributes.Add("onClick"," return
ValidateDate(document.GetElementById('" +
txtFromDate.ClentId() + ').Value",document.GetElementById('" +
txtToDate.ClentId()+ "'));");
 
S

settyv

Hi,

Here is the ASPX code.

<body ms_positioning="gridlayout">
<TABLE height="471" cellSpacing="0" cellPadding="0" width="174"
border="0" ms_2d_layout="TRUE">
<TR vAlign="top">
<TD width="174" height="471">
<form id="form1" runat="server">
<TABLE height="157" cellSpacing="0" cellPadding="0" width="459"
border="0" ms_2d_layout="TRUE">
<TR vAlign="top">
<TD colSpan="3" height="1"></TD>
<TD colSpan="2" rowSpan="2"><asp:textbox id="txtToDate"
runat="server"></asp:textbox></TD>
</TR>
<TR vAlign="top">
<TD height="38"></TD>
<TD colSpan="2"><asp:label id="lblFDate" runat="server"
Width="87px">From Date:</asp:label></TD>
</TR>
<TR vAlign="top">
<TD colSpan="2" height="1"></TD>
<TD colSpan="3" rowSpan="2"><asp:textbox id="txtFromDate"
runat="server"></asp:textbox></TD>
</TR>
<TR vAlign="top">
<TD height="41"></TD>
<TD><asp:label id="lblTDate" runat="server" Width="83px">To
Date:</asp:label></TD>
</TR>
<TR vAlign="top">
<TD colSpan="4" height="25"></TD>
<TD><asp:button id="btnSubmit" runat="server"
Text="Submit"></asp:button></TD>
</TR>
</TABLE>
</form>
</TD>
</TR>
</TABLE>
</body>

And am trying to call the javascript that i sent earlier in code-behind
as shown below:

btnSubmit.Attributes.Add("onClick","return
ValidateDate(document.form1.elements['" + txtFromDate.ClientID +
"'].value,document.form1.elements['"
+ txtToDate.ClientID + "'].value);");


Now the problem is that am not able to see alert msg saying that the
difference is 0 ,which takes both dates as current date and it returns
the difference as 0. Please let me know how can i solve this issue.

Thanks,
Vishnu



Milosz said:
Show us more code (espacially text boxes declaration)
--
Milosz Skalecki
MCP, MCAD


Hi,

It is not working .it is telling script error.Here is the HTML created
after the postback.Everything is fine.But i dont know why it is telling
error.

Microsoft JScript runtime error: 'document.GetElementById.txtFromDate'
is null or not an object

<TR vAlign="top">
<TD colSpan="4" height="25"></TD>
<TD><input type="submit" name="btnSubmit" value="Submit"
id="btnSubmit" onClick="return
ValidateDate(document.getElementById["txtFromDate"].Value,document.getElementById["txtToDate"].Value);"
/></TD>
</TR>

Please let me know how can i solve this.


Thanks,
Vishnu

Try this

btnSubmit.Attributes.Add("onClick"," return
ValidateDate(document.GetElementById('" +
txtFromDate.ClentId() + ').Value",document.GetElementById('" +
txtToDate.ClentId()+ "'));");



(e-mail address removed) wrote:
Hi,

Below is the Javascript function that am trying to call from asp:Button
control.

<script language="javascript">
function ValidateDate(fromDate,toDate)
{

var fromDate=new Date();
var toDate=new Date();
var diffDate = Math.round( ( toDate-fromDate ) / 864e5 );
alert(diffDate);
if( diffDate < 0 )
{
alert( "The start date cannot be after the end date!")
return false;
}

if( diffDate > 30 )
{
alert( "Please enter dates less than 31 days apart" )
return false;
}

return true; //allow submit
}
</script>


In code-behind am calling the function like as below:
btnSubmit.Attributes.Add("onClick"," return ValidateDate(" +
txtFromDate.Text.ToString() + "," + txtToDate.Text.ToString() + ");");



But if start entering the dates,it submits the page as it takes both
datee as current dates.Please let me know the reason.

Thanks,
Vishnu
 
G

Guest

You're assigning both dates to current date:
fromDate = new Date();
toDate = new Date();
that's why you always get 0 in difference.

it should look like this:
-- begin javascript code --

function ValidateDate(fromDateStr,toDateStr)
{

var fromDate = new Date(fromDateStr) ;
var toDate = new Date(toDateStr);
var diffDate = Math.round( ( toDate-fromDate ) / 864e5 );

alert(fromDate.toGMTString() + "\n" + toDate.toGMTString() + "\n" +
diffDate);

if( diffDate < 0 )
{
alert( "The start date cannot be after the end date!");
return false;
}

if( diffDate > 30 )
{
alert( "Please enter dates less than 31 days apart" )
return false;
}

return true; //allow submit
}

-- end javascript code --

and the Page_Load in code behind:

--- begin c# code --

btnSubmit.Attributes.Add("onClick", String.Format("return
ValidateDate(document.getElementById('{0}').value,
document.getElementById('{1}').value);",
txtFromDate.ClientID , txtToDate.ClientID));
--- end c# code --

Before you start working with dates in javascript please familirize yourself
with potential problems since date strings are always ambiguous, (i.e. you
should use
new Date (year, month [, date [, hours [, minutes [, seconds [, ms ]]]]])

hope this helps

--
Milosz Skalecki
MCP, MCAD


Hi,

Here is the ASPX code.

<body ms_positioning="gridlayout">
<TABLE height="471" cellSpacing="0" cellPadding="0" width="174"
border="0" ms_2d_layout="TRUE">
<TR vAlign="top">
<TD width="174" height="471">
<form id="form1" runat="server">
<TABLE height="157" cellSpacing="0" cellPadding="0" width="459"
border="0" ms_2d_layout="TRUE">
<TR vAlign="top">
<TD colSpan="3" height="1"></TD>
<TD colSpan="2" rowSpan="2"><asp:textbox id="txtToDate"
runat="server"></asp:textbox></TD>
</TR>
<TR vAlign="top">
<TD height="38"></TD>
<TD colSpan="2"><asp:label id="lblFDate" runat="server"
Width="87px">From Date:</asp:label></TD>
</TR>
<TR vAlign="top">
<TD colSpan="2" height="1"></TD>
<TD colSpan="3" rowSpan="2"><asp:textbox id="txtFromDate"
runat="server"></asp:textbox></TD>
</TR>
<TR vAlign="top">
<TD height="41"></TD>
<TD><asp:label id="lblTDate" runat="server" Width="83px">To
Date:</asp:label></TD>
</TR>
<TR vAlign="top">
<TD colSpan="4" height="25"></TD>
<TD><asp:button id="btnSubmit" runat="server"
Text="Submit"></asp:button></TD>
</TR>
</TABLE>
</form>
</TD>
</TR>
</TABLE>
</body>

And am trying to call the javascript that i sent earlier in code-behind
as shown below:

btnSubmit.Attributes.Add("onClick","return
ValidateDate(document.form1.elements['" + txtFromDate.ClientID +
"'].value,document.form1.elements['"
+ txtToDate.ClientID + "'].value);");


Now the problem is that am not able to see alert msg saying that the
difference is 0 ,which takes both dates as current date and it returns
the difference as 0. Please let me know how can i solve this issue.

Thanks,
Vishnu



Milosz said:
Show us more code (espacially text boxes declaration)
--
Milosz Skalecki
MCP, MCAD


Hi,

It is not working .it is telling script error.Here is the HTML created
after the postback.Everything is fine.But i dont know why it is telling
error.

Microsoft JScript runtime error: 'document.GetElementById.txtFromDate'
is null or not an object

<TR vAlign="top">
<TD colSpan="4" height="25"></TD>
<TD><input type="submit" name="btnSubmit" value="Submit"
id="btnSubmit" onClick="return
ValidateDate(document.getElementById["txtFromDate"].Value,document.getElementById["txtToDate"].Value);"
/></TD>
</TR>

Please let me know how can i solve this.


Thanks,
Vishnu

(e-mail address removed) wrote:
Try this

btnSubmit.Attributes.Add("onClick"," return
ValidateDate(document.GetElementById('" +
txtFromDate.ClentId() + ').Value",document.GetElementById('" +
txtToDate.ClentId()+ "'));");



(e-mail address removed) wrote:
Hi,

Below is the Javascript function that am trying to call from asp:Button
control.

<script language="javascript">
function ValidateDate(fromDate,toDate)
{

var fromDate=new Date();
var toDate=new Date();
var diffDate = Math.round( ( toDate-fromDate ) / 864e5 );
alert(diffDate);
if( diffDate < 0 )
{
alert( "The start date cannot be after the end date!")
return false;
}

if( diffDate > 30 )
{
alert( "Please enter dates less than 31 days apart" )
return false;
}

return true; //allow submit
}
</script>


In code-behind am calling the function like as below:
btnSubmit.Attributes.Add("onClick"," return ValidateDate(" +
txtFromDate.Text.ToString() + "," + txtToDate.Text.ToString() + ");");



But if start entering the dates,it submits the page as it takes both
datee as current dates.Please let me know the reason.

Thanks,
Vishnu
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top