473,406 Members | 2,387 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Help - client side validation not working, driving me MAD!

Hello,

I have some simple client-side Javascript validation that was working
fine until now. For some reason (don't ask me), it has stopped working
and I can't figure out why. I have switched to version 2.0 (from 1.1)
and have changed the page to use a master page, but other than that,
it's the same.

I am trying to validate the from date for a credit card to make sure the
date has already passed. The code is pretty straightforward. I have two
dropdown controls and a custom validator...

<asp:DropDownList ID="drpFromMonth" RunAt="server">
<asp:ListItem value="01">01/January</asp:ListItem>
.... etc ...
<asp:ListItem value="12">12/December</asp:ListItem>
</asp:DropDownList>

<asp:DropDownList ID="drpFromYear" RunAt="server" />

<asp:CustomValidator ControlToValidate="drpFromMonth"
OnServerValidate="ValidateValidFrom"
ClientValidationFunction="ValidateValidFromDate" Display="Dynamic"
ErrorMessage="not valid yet" Text="not valid yet" Runat="Server" />

The values for drpFromYear are set in code. The client-side function
looks like this...

function ValidateValidFromDate(s, e) {
fromMonth = parseInt(document.aspnetForm.drpFromMonth.selected Index);
fromYear = parseInt(document.aspnetForm.drpFromYear.options[document.aspnetForm.drpFromYear.selectedIndex].value);
now = new Date;
nowMonth = now.getMonth();
nowYear = now.getYear();
dateOK = false;
if (fromYear > nowYear) {
dateOK = false;
}
if (fromYear == nowYear) {
if (fromMonth <= nowMonth) {
dateOK = true;
} else {
dateOK = false;
}
}
if (fromYear < nowYear) {
dateOK = true;
}
e.IsValid = dateOK;
}

I originally had document.form1.drpFromMonth in there, which worked.
Since I had this error, I tried changing it to
document.aspnetForm.drpFromMonth as that uses the form name that is
generated by the framework. I tried setting the form's id myself, but it
was ignored. Whatever I did, the generated page had aspnetForm as the
form name and ID. I changed my client-side code to use this, but it
still didn't work.

When I try the page, I get an error...

document.aspnetForm.drpFromMonth.selectedIndex is null or not an object

I have tried various variations, like document.forms[0].drpFromMonth and
document.all["drpFromMonth"], but with the same result.

Anyone any idea how to get this to work? TIA

--
Alan Silver
(anything added below this line is nothing to do with me)
Dec 5 '05 #1
9 2207
Hi Alan,

Can you try RegisterClientScript method, in the Page_Load event?

Thanks,
Rajeev Gopal
www.geekswithblogs.net/rajeevgopal

Dec 5 '05 #2
>I have some simple client-side Javascript validation that was working
fine until now. For some reason (don't ask me), it has stopped working
and I can't figure out why. I have switched to version 2.0 (from 1.1)
and have changed the page to use a master page, but other than that,
it's the same.


OK, I think I have found out the problem more specifically, but haven't
yet found an answer. Maybe if I clarify what's happening, someone may be
able to help.

Consider the following simple (air) code...

</form id="form1" runat="server">
<asp:Textbox id="txtName" runat="server" />
</form>

When used on a normal (master-less) page, the HTML that is emitted looks
something like this...

<form action="http://whatever/page.aspx" method="post">
<input type="text" id="txtName">
</form>

If you now use the same code in a master page, it looks very different.
Specifically, the id of the textbox is *not* the same as the one you
specified, it is something like "ctl00_cphMainBody_txtName", where
"cphMainBody" is the id of the contentplaceholder control in the master
page.

Furthermore, any id you give the form is ignored, and the id is set to
"aspnetForm". This looks like a bug in the framework to me. Sure I can
use "aspnetForm" in my Javascript, but who says that autogenerated name
won't change in another version of the framework? At least when I set
the id, I know what it will be. I can find ways around that (like using
document.forms[0]), but it still leaves the more serious problem...

How do I get a reference to the text box in client-side Javascript? I
can't predict what the text box's id will be. Any client-side validation
that needs to reference a control not passed as a parameter cannot be
referenced.

Anyone any comments? This seems like a major problem with using master
pages. I can see *why* it's happening, but I can't see any way of
getting around it.

--
Alan Silver
(anything added below this line is nothing to do with me)
Dec 6 '05 #3
>OK, I think I have found out the problem more specifically, but haven't
yet found an answer.


<g>Predictably, I found an answer shortly after posting.

http://forums.asp.net/1119696/ShowPost.aspx explains that you need to
use server-side code to write the ClientID of the control into the
Javascript. I found that the code shown there didn't work, but when
changed to...

alert(document.forms[0].<%= txtName.ClientID %>.value);

it worked fine. When the page is rendered, the Javascript looks
something like...

alert(document.forms[0].ctl00_cphMainBody_txtName.value);

which is what I wanted ;-)

I still reckon the other issue with the id of the form tag is a bug
though. I would be interested to hear anyone else's opinion on this.

--
Alan Silver
(anything added below this line is nothing to do with me)
Dec 6 '05 #4
me.textbox1.clientid

insert it in js in <%= %> tags
"Alan Silver" <al*********@nospam.thanx> schreef in bericht
news:cj**************@nospamthankyou.spam...
Hello,

I have some simple client-side Javascript validation that was working fine
until now. For some reason (don't ask me), it has stopped working and I
can't figure out why. I have switched to version 2.0 (from 1.1) and have
changed the page to use a master page, but other than that, it's the same.

I am trying to validate the from date for a credit card to make sure the
date has already passed. The code is pretty straightforward. I have two
dropdown controls and a custom validator...

<asp:DropDownList ID="drpFromMonth" RunAt="server">
<asp:ListItem value="01">01/January</asp:ListItem>
... etc ...
<asp:ListItem value="12">12/December</asp:ListItem>
</asp:DropDownList>

<asp:DropDownList ID="drpFromYear" RunAt="server" />

<asp:CustomValidator ControlToValidate="drpFromMonth"
OnServerValidate="ValidateValidFrom"
ClientValidationFunction="ValidateValidFromDate" Display="Dynamic"
ErrorMessage="not valid yet" Text="not valid yet" Runat="Server" />

The values for drpFromYear are set in code. The client-side function looks
like this...

function ValidateValidFromDate(s, e) {
fromMonth = parseInt(document.aspnetForm.drpFromMonth.selected Index);
fromYear =
parseInt(document.aspnetForm.drpFromYear.options[document.aspnetForm.drpFromYear.selectedIndex].value);
now = new Date;
nowMonth = now.getMonth();
nowYear = now.getYear();
dateOK = false;
if (fromYear > nowYear) {
dateOK = false;
}
if (fromYear == nowYear) {
if (fromMonth <= nowMonth) {
dateOK = true;
} else {
dateOK = false;
}
}
if (fromYear < nowYear) {
dateOK = true;
}
e.IsValid = dateOK;
}

I originally had document.form1.drpFromMonth in there, which worked. Since
I had this error, I tried changing it to
document.aspnetForm.drpFromMonth as that uses the form name that is
generated by the framework. I tried setting the form's id myself, but it
was ignored. Whatever I did, the generated page had aspnetForm as the form
name and ID. I changed my client-side code to use this, but it still
didn't work.

When I try the page, I get an error...

document.aspnetForm.drpFromMonth.selectedIndex is null or not an object

I have tried various variations, like document.forms[0].drpFromMonth and
document.all["drpFromMonth"], but with the same result.

Anyone any idea how to get this to work? TIA

--
Alan Silver
(anything added below this line is nothing to do with me)

Dec 6 '05 #5
Alan,

I can't tell from your sample code, but you will need to
programmatically build your JS function "ValidateValidFromDate". As
you programmatically build that function, instead of "hardwiring" the
reference to the drop down list by doing this:
"document.aspnetForm.drpFromMonth". You should reference ClientId
properties of your controls to get the actual name that will appear in
HTML, substitute those in your function.

For example, you could do something like the following (literalJSScript
is an ASP.NET Literal server control placed on the page somewhere):

literalJScript.Text += "<script language=\"javaScript\" >";
literalJScript.Text += "function ValidateValidFromDate() { ";
literalJScript.Text += " var drpFormDate = document." +
this.Form.ClientId+"[\"" + drpFromDate.ClientID +"\"]; ";

Does this make sense?

Dec 6 '05 #6
<snip>
Does this make sense?


Lots!! It's very similar to the answer I found and posted just a few
minutes ago (which may not have appeared on the server yet). The way I
was doing was to use <%= txtName.ClientID %> in an otherwise normal
<script> block, but the principle is the same. I prefer the way I
showed, simply because the Javascript is easier to read, so easier to
debug. Both would work as well.

Thanks for the reply.

--
Alan Silver
(anything added below this line is nothing to do with me)
Dec 6 '05 #7
>me.textbox1.clientid

insert it in js in <%= %> tags
Thanks, I found that just after posting (and posted it for others to
see, but it might not be visible yet).

Thanks very much for the reply.
"Alan Silver" <al*********@nospam.thanx> schreef in bericht
news:cj**************@nospamthankyou.spam...
Hello,

I have some simple client-side Javascript validation that was working fine
until now. For some reason (don't ask me), it has stopped working and I
can't figure out why. I have switched to version 2.0 (from 1.1) and have
changed the page to use a master page, but other than that, it's the same.

I am trying to validate the from date for a credit card to make sure the
date has already passed. The code is pretty straightforward. I have two
dropdown controls and a custom validator...

<asp:DropDownList ID="drpFromMonth" RunAt="server">
<asp:ListItem value="01">01/January</asp:ListItem>
... etc ...
<asp:ListItem value="12">12/December</asp:ListItem>
</asp:DropDownList>

<asp:DropDownList ID="drpFromYear" RunAt="server" />

<asp:CustomValidator ControlToValidate="drpFromMonth"
OnServerValidate="ValidateValidFrom"
ClientValidationFunction="ValidateValidFromDate" Display="Dynamic"
ErrorMessage="not valid yet" Text="not valid yet" Runat="Server" />

The values for drpFromYear are set in code. The client-side function looks
like this...

function ValidateValidFromDate(s, e) {
fromMonth = parseInt(document.aspnetForm.drpFromMonth.selected Index);
fromYear =

parseInt(document.aspnetForm.drpFromYear.optio ns[document.aspnetForm.dr
pFromYear.selectedIndex].value);
now = new Date;
nowMonth = now.getMonth();
nowYear = now.getYear();
dateOK = false;
if (fromYear > nowYear) {
dateOK = false;
}
if (fromYear == nowYear) {
if (fromMonth <= nowMonth) {
dateOK = true;
} else {
dateOK = false;
}
}
if (fromYear < nowYear) {
dateOK = true;
}
e.IsValid = dateOK;
}

I originally had document.form1.drpFromMonth in there, which worked. Since
I had this error, I tried changing it to
document.aspnetForm.drpFromMonth as that uses the form name that is
generated by the framework. I tried setting the form's id myself, but it
was ignored. Whatever I did, the generated page had aspnetForm as the form
name and ID. I changed my client-side code to use this, but it still
didn't work.

When I try the page, I get an error...

document.aspnetForm.drpFromMonth.selectedIndex is null or not an object

I have tried various variations, like document.forms[0].drpFromMonth and
document.all["drpFromMonth"], but with the same result.

Anyone any idea how to get this to work? TIA

--
Alan Silver
(anything added below this line is nothing to do with me)



--
Alan Silver
(anything added below this line is nothing to do with me)
Dec 6 '05 #8
Alan,

With respect to the formName....that's not a bug. It comes from the
fact that the form on your ASPX page is not named with the "id"
attribute. If the "id" is not specified ASP.NET assigns a default name
to the form. In previous versions of ASP.NET the default was the name
of the class (which I like better), but in ASP.NET 2.0 the default name
is somewhat non-deterministic. If you use need the form's name, you
can use the ClientId property of that as well.

I'm working through this exact problem right now because we have
automated testing tools which depend on the full JavaScript object name
of the form. I'm adding code to our base page class that will assign a
better ID to the form. If interested, here it is:

if (this.Form != null && this.Form.ID == null)
{
string pageName = this.GetType().BaseType.Name;
this.Form.ID = pageName;
}

--steve

Dec 6 '05 #9
Steve,

Thanks for the reply. I was surprised to think that such a bug could
have got this far!! See below...
With respect to the formName....that's not a bug. It comes from the
fact that the form on your ASPX page is not named with the "id"
attribute. If the "id" is not specified ASP.NET assigns a default name
to the form. In previous versions of ASP.NET the default was the name
of the class (which I like better),
Hmm, my 1.1 code set the id of the form and that was used, not the class
name. I had the opening part of the form tag in a user control, so it
was common to all pages. I always got an id of "form1", which was what I
set.
but in ASP.NET 2.0 the default name
is somewhat non-deterministic. If you use need the form's name, you
can use the ClientId property of that as well.


I actually found it worked better using document.forms[0] instead of the
form id.

<snip>

Thanks for the reply

--
Alan Silver
(anything added below this line is nothing to do with me)
Dec 6 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: abcd | last post by:
kutthaense Secretary Djetvedehald H. Rumsfeld legai predicted eventual vicmadhlary in Iraq mariyu Afghmadhlaistmadhla, kaani jetvedehly after "a ljetvedehg, hard slog," mariyu vede legai pressed...
14
by: Matt | last post by:
I want to know if ASP.NET Web Forms Validation Controls are Server-Side or Client-Side form validation? Since I think each validator control can select either 1) JavaScript based error dialog or 2)...
4
by: vzaffiro | last post by:
Our Development Server (windows 2003, framework 1.1) was working great for months, then one day all the client side validators stopped working. Only the server side validation is working. Our code...
3
by: Guadala Harry | last post by:
I hope this is something I can solve without contacting the vendor: I'm implementing a 3rd party component that will, by itself, initiate a PostBack. Apparently it does not support the...
1
by: Hong Hao | last post by:
Recently, I was trying to modify an existing aspx page when client-side validation on that page stopped working. I searched this group and the web in general and found that other people have had...
4
by: devanoy | last post by:
I have a piece of c# code in the Page_Load function. submitButton.Attributes.Add("onClick", "return checkForm('" + this + "','" + SomeTextBox.Text + "')"); When I click the submitButton, it...
6
by: serge calderara | last post by:
Dear all, I have read that ASP.NET does double user input validation of control when they are place on the page. Once on teh client side and again from server side right ? Could explain how...
3
by: doc | last post by:
What will a flash xml client socket connect to? I have a working php TCP/IP server socket bound to a port >1023 and the flash client will not even connect to it. I can connect to it with non-xml...
2
by: Chad Lupkes | last post by:
Hi everyone, I need help with a simple form validation. The form I'm using has been the target of some spammers, and I'm wondering what else I can do. I have a very simple validation, checking...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.