473,757 Members | 10,007 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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:DropDownLi st ID="drpFromMont h" RunAt="server">
<asp:ListItem value="01">01/January</asp:ListItem>
.... etc ...
<asp:ListItem value="12">12/December</asp:ListItem>
</asp:DropDownLis t>

<asp:DropDownLi st ID="drpFromYear " RunAt="server" />

<asp:CustomVali dator ControlToValida te="drpFromMont h"
OnServerValidat e="ValidateVali dFrom"
ClientValidatio nFunction="Vali dateValidFromDa te" Display="Dynami c"
ErrorMessage="n ot valid yet" Text="not valid yet" Runat="Server" />

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

function ValidateValidFr omDate(s, e) {
fromMonth = parseInt(docume nt.aspnetForm.d rpFromMonth.sel ectedIndex);
fromYear = parseInt(docume nt.aspnetForm.d rpFromYear.opti ons[document.aspnet Form.drpFromYea r.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.aspnet Form.drpFromMon th 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.aspnet Form.drpFromMon th.selectedInde x is null or not an object

I have tried various variations, like document.forms[0].drpFromMonth and
document.all["drpFromMon th"], 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 2237
Hi Alan,

Can you try RegisterClientS cript 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_cphMainB ody_txtName", where
"cphMainBod y" is the id of the contentplacehol der 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>Predictabl y, 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.ClientI D %>.value);

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

alert(document. forms[0].ctl00_cphMainB ody_txtName.val ue);

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.cli entid

insert it in js in <%= %> tags
"Alan Silver" <al*********@no spam.thanx> schreef in bericht
news:cj******** ******@nospamth ankyou.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:DropDownLi st ID="drpFromMont h" RunAt="server">
<asp:ListItem value="01">01/January</asp:ListItem>
... etc ...
<asp:ListItem value="12">12/December</asp:ListItem>
</asp:DropDownLis t>

<asp:DropDownLi st ID="drpFromYear " RunAt="server" />

<asp:CustomVali dator ControlToValida te="drpFromMont h"
OnServerValidat e="ValidateVali dFrom"
ClientValidatio nFunction="Vali dateValidFromDa te" Display="Dynami c"
ErrorMessage="n ot valid yet" Text="not valid yet" Runat="Server" />

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

function ValidateValidFr omDate(s, e) {
fromMonth = parseInt(docume nt.aspnetForm.d rpFromMonth.sel ectedIndex);
fromYear =
parseInt(docume nt.aspnetForm.d rpFromYear.opti ons[document.aspnet Form.drpFromYea r.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.aspnet Form.drpFromMon th 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.aspnet Form.drpFromMon th.selectedInde x is null or not an object

I have tried various variations, like document.forms[0].drpFromMonth and
document.all["drpFromMon th"], 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
programmaticall y build your JS function "ValidateValidF romDate". As
you programmaticall y build that function, instead of "hardwiring " the
reference to the drop down list by doing this:
"document.aspne tForm.drpFromMo nth". 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 (literalJSScrip t
is an ASP.NET Literal server control placed on the page somewhere):

literalJScript. Text += "<script language=\"java Script\" >";
literalJScript. Text += "function ValidateValidFr omDate() { ";
literalJScript. Text += " var drpFormDate = document." +
this.Form.Clien tId+"[\"" + drpFromDate.Cli entID +"\"]; ";

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.ClientI D %> 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.cl ientid

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*********@no spam.thanx> schreef in bericht
news:cj******* *******@nospamt hankyou.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:DropDownLi st ID="drpFromMont h" RunAt="server">
<asp:ListItem value="01">01/January</asp:ListItem>
... etc ...
<asp:ListItem value="12">12/December</asp:ListItem>
</asp:DropDownLis t>

<asp:DropDownLi st ID="drpFromYear " RunAt="server" />

<asp:CustomVali dator ControlToValida te="drpFromMont h"
OnServerValidat e="ValidateVali dFrom"
ClientValidatio nFunction="Vali dateValidFromDa te" Display="Dynami c"
ErrorMessage="n ot valid yet" Text="not valid yet" Runat="Server" />

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

function ValidateValidFr omDate(s, e) {
fromMonth = parseInt(docume nt.aspnetForm.d rpFromMonth.sel ectedIndex);
fromYear =

parseInt(docu ment.aspnetForm .drpFromYear.op tions[document.aspnet Form.dr
pFromYear.sel ectedIndex].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.aspnet Form.drpFromMon th 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.aspnet Form.drpFromMon th.selectedInde x is null or not an object

I have tried various variations, like document.forms[0].drpFromMonth and
document.all["drpFromMon th"], 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....tha t'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....tha t'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
3473
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 Pentagjetvedeh karuvificials madhla reachathe strategy in karkun campaign deshatinst terrorism. "mudivae maretu winning or losing karkun global varti jetvedeh terror?" Mr. Rumsfeld adugued in a recent memormariyuum. vede velli jetvedeh madhla...
14
6310
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) show the error message next to the control. For example, if the text field is empty with RequiredField Validator control, it can show the value in ControlToValidate property in two ways as I mentioned. Please advise. Thanks!
4
1903
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 was ported to other servers and the client side validation works fine. This makes me believe that the .net framework is not working correctly or a service pack or hot fix was installed. Has anyone seen this problem?
3
2971
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 CausesValidation property - so forms get posted back without any client-side validation. I know I can do validation server side (and do anyway just as a good practice), but I want this control (which is basically an ImageButton) to trigger client-side...
1
3950
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 the same issue. However, none of the suggested fixes solved my particular problem. I tracked down the cause of the problem, which is related to aspx page parser's handling of controls inside html comments. The problem may be quite common and well...
4
1501
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 calls the javascript function, but the value is not being passed.... I cannot find a work around.
6
1438
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 this process is exaclty working ? regards thnaks for your help
3
3421
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 client. Can anyone explain this to me - it has been driving me mad for weeks! Please help! Thanks
2
1467
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 for blanks, and found an additional zipcode validation form here: http://javascript.internet.com/forms/val-zip-code.html I tried combining them, and it doesn't seem to be working. I'm not
0
9298
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10072
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9906
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9737
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7286
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6562
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5172
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5329
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3399
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.