473,808 Members | 2,745 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Javascript question

Hi,
I have an asp.net form where if the user enters an
incorect date I change the color of the textbox to red and
also display a label field with a message saying the date
is invalid. What I would like to do is to have some way to
trap the event where the user changes the date and reset
the color of the textbox to white and change the error
lablel to hidden. I want to do this before the page gets
submitted back to the server so I've tried various
Javascript routines but can't seem to get this to work.
(I'm using a date field here as example but the textbox
could be anything I ant to edit in the code-behind).

Here is the latest thing I tried, but no luck.

<body MS_POSITIONING= "GridLayout ">
<form id="Form1" method="post" runat="server"
onsubmit="Chang eColor()">

<asp:TextBox id="tbDate" style="Z-INDEX:
120; LEFT: 240px; POSITION: absolute; TOP: 191px"
tabIndex="6" runat="server" Width="86px"></asp:TextBox>
<asp:Label id="lblDate" style="Z-INDEX:
119; LEFT: 45px; POSITION: absolute; TOP: 195px"
runat="server"> As of Date </asp:Label>
<asp:button id="btnOK" style="Z-INDEX:
103; LEFT: 333px; POSITION: absolute; TOP: 393px"
tabIndex="18" runat="server" Width="61px"
Text="OK"></asp:button>
<asp:button id="btnCancel" style="Z-INDEX:
106; LEFT: 432px; POSITION: absolute; TOP: 393px"
tabIndex="19" runat="server" Width="61"
Text="Cancel"></asp:button>
<asp:label id="lblError" style="Z-INDEX:
101; LEFT: 32px; POSITION: absolute; TOP: 431px"
runat="server" Visible="False" Width="565px"></asp:label>
</form>

<script language="javas cript">
function ChangeColor()
{
document.forms( 0).elements
['lblError'].Hidden = 'True';
// document.forms( 0).tbDate.BackC olor
= "White";
}
</script>
</body>

Thanks,
JOhn Ritchie
Nov 17 '05 #1
5 1829
In ASP.NET there are field validators. Drag one onto the screen and assign
it to your control. You can specify whether you want it to run client-side
(so it tells your right away when something is wrong) or server-side (where
it processes all elements and shows you all-at-once what's wrong). These are
a super time-saver...

hth

"John" <jc*******@dash group.com> wrote in message
news:00******** *************** *****@phx.gbl.. .
Hi,
I have an asp.net form where if the user enters an
incorect date I change the color of the textbox to red and
also display a label field with a message saying the date
is invalid. What I would like to do is to have some way to
trap the event where the user changes the date and reset
the color of the textbox to white and change the error
lablel to hidden. I want to do this before the page gets
submitted back to the server so I've tried various
Javascript routines but can't seem to get this to work.
(I'm using a date field here as example but the textbox
could be anything I ant to edit in the code-behind).

Here is the latest thing I tried, but no luck.

<body MS_POSITIONING= "GridLayout ">
<form id="Form1" method="post" runat="server"
onsubmit="Chang eColor()">

<asp:TextBox id="tbDate" style="Z-INDEX:
120; LEFT: 240px; POSITION: absolute; TOP: 191px"
tabIndex="6" runat="server" Width="86px"></asp:TextBox>
<asp:Label id="lblDate" style="Z-INDEX:
119; LEFT: 45px; POSITION: absolute; TOP: 195px"
runat="server"> As of Date </asp:Label>
<asp:button id="btnOK" style="Z-INDEX:
103; LEFT: 333px; POSITION: absolute; TOP: 393px"
tabIndex="18" runat="server" Width="61px"
Text="OK"></asp:button>
<asp:button id="btnCancel" style="Z-INDEX:
106; LEFT: 432px; POSITION: absolute; TOP: 393px"
tabIndex="19" runat="server" Width="61"
Text="Cancel"></asp:button>
<asp:label id="lblError" style="Z-INDEX:
101; LEFT: 32px; POSITION: absolute; TOP: 431px"
runat="server" Visible="False" Width="565px"></asp:label>
</form>

<script language="javas cript">
function ChangeColor()
{
document.forms( 0).elements
['lblError'].Hidden = 'True';
// document.forms( 0).tbDate.BackC olor
= "White";
}
</script>
</body>

Thanks,
JOhn Ritchie

Nov 17 '05 #2
Hi John,

It seems to me that you are using the tools that are already available to you.
You can add a regular expression validator, set it to dynamic and do what you
need without writing any code. It even switches the error message on and off
using its own Javascript.

I've put some sample code below. You may want to find a regular expression that
more closely meets your needs here:
http://www.regxlib.com/DisplayPatter...4&categoryId=5

Does this help?

Ken
MVP [ASP.NET]
<body>
<form id="Form1" method="post" runat="server" onsubmit="Chang eColor()">
<p>
<asp:label id="lblDate" runat="server"> As of Date </asp:label>
<asp:textbox id="tbDate" tabindex="6" runat="server"
width="86px"></asp:textbox></p>
<p>
<asp:button id="btnOK" tabindex="18" runat="server" width="61px"
text="OK"></asp:button>
<asp:button id="btnCancel" tabindex="19" runat="server" width="61"
text="Cancel"></asp:button></p>
<p>
<asp:regularexp ressionvalidato r id="RegularExpr essionValidator 1"
runat="server" errormessage="P lease check the date value"
controltovalida te="tbDate"
display="Dynami c" validationexpre ssion="^\d{1,2} \/\d{1,2}\/\d{4}">

</asp:regularexpr essionvalidator >
</p>
</form>
</body>
--
Microsoft MVPs have a question for *you*: Are you patched against the Worm?
http://www.microsoft.com/security/se...s/ms03-026.asp

"John" <jc*******@dash group.com> wrote in message
news:00******** *************** *****@phx.gbl.. .
Hi,
I have an asp.net form where if the user enters an
incorect date I change the color of the textbox to red and
also display a label field with a message saying the date
is invalid. What I would like to do is to have some way to
trap the event where the user changes the date and reset
the color of the textbox to white and change the error
lablel to hidden. I want to do this before the page gets
submitted back to the server so I've tried various
Javascript routines but can't seem to get this to work.
(I'm using a date field here as example but the textbox
could be anything I ant to edit in the code-behind).

Here is the latest thing I tried, but no luck.

<body MS_POSITIONING= "GridLayout ">
<form id="Form1" method="post" runat="server"
onsubmit="Chang eColor()">

<asp:TextBox id="tbDate" style="Z-INDEX:
120; LEFT: 240px; POSITION: absolute; TOP: 191px"
tabIndex="6" runat="server" Width="86px"></asp:TextBox>
<asp:Label id="lblDate" style="Z-INDEX:
119; LEFT: 45px; POSITION: absolute; TOP: 195px"
runat="server"> As of Date </asp:Label>
<asp:button id="btnOK" style="Z-INDEX:
103; LEFT: 333px; POSITION: absolute; TOP: 393px"
tabIndex="18" runat="server" Width="61px"
Text="OK"></asp:button>
<asp:button id="btnCancel" style="Z-INDEX:
106; LEFT: 432px; POSITION: absolute; TOP: 393px"
tabIndex="19" runat="server" Width="61"
Text="Cancel"></asp:button>
<asp:label id="lblError" style="Z-INDEX:
101; LEFT: 32px; POSITION: absolute; TOP: 431px"
runat="server" Visible="False" Width="565px"></asp:label>
</form>

<script language="javas cript">
function ChangeColor()
{
document.forms( 0).elements
['lblError'].Hidden = 'True';
// document.forms( 0).tbDate.BackC olor
= "White";
}
</script>
</body>

Thanks,
JOhn Ritchie
Nov 17 '05 #3
I meant "aren't" using the tools...

--
Microsoft MVPs have a question for *you*: Are you patched against the Worm?
http://www.microsoft.com/security/se...s/ms03-026.asp

"Ken Cox [Microsoft MVP]" <BA************ @sympatico.ca> wrote in message
news:u$******** ******@tk2msftn gp13.phx.gbl...
Hi John,

It seems to me that you are using the tools that are already available to you.
You can add a regular expression validator, set it to dynamic and do what you
need without writing any code. It even switches the error message on and off
using its own Javascript.

I've put some sample code below. You may want to find a regular expression that
more closely meets your needs here:
http://www.regxlib.com/DisplayPatter...4&categoryId=5

Does this help?

Ken
MVP [ASP.NET]
<body>
<form id="Form1" method="post" runat="server" onsubmit="Chang eColor()">
<p>
<asp:label id="lblDate" runat="server"> As of Date </asp:label>
<asp:textbox id="tbDate" tabindex="6" runat="server"
width="86px"></asp:textbox></p>
<p>
<asp:button id="btnOK" tabindex="18" runat="server" width="61px"
text="OK"></asp:button>
<asp:button id="btnCancel" tabindex="19" runat="server" width="61"
text="Cancel"></asp:button></p>
<p>
<asp:regularexp ressionvalidato r id="RegularExpr essionValidator 1"
runat="server" errormessage="P lease check the date value"
controltovalida te="tbDate"
display="Dynami c" validationexpre ssion="^\d{1,2} \/\d{1,2}\/\d{4}">

</asp:regularexpr essionvalidator >
</p>
</form>
</body>
--
Microsoft MVPs have a question for *you*: Are you patched against the Worm?
http://www.microsoft.com/security/se...s/ms03-026.asp

"John" <jc*******@dash group.com> wrote in message
news:00******** *************** *****@phx.gbl.. .
Hi,
I have an asp.net form where if the user enters an
incorect date I change the color of the textbox to red and
also display a label field with a message saying the date
is invalid. What I would like to do is to have some way to
trap the event where the user changes the date and reset
the color of the textbox to white and change the error
lablel to hidden. I want to do this before the page gets
submitted back to the server so I've tried various
Javascript routines but can't seem to get this to work.
(I'm using a date field here as example but the textbox
could be anything I ant to edit in the code-behind).

Here is the latest thing I tried, but no luck.

<body MS_POSITIONING= "GridLayout ">
<form id="Form1" method="post" runat="server"
onsubmit="Chang eColor()">

<asp:TextBox id="tbDate" style="Z-INDEX:
120; LEFT: 240px; POSITION: absolute; TOP: 191px"
tabIndex="6" runat="server" Width="86px"></asp:TextBox>
<asp:Label id="lblDate" style="Z-INDEX:
119; LEFT: 45px; POSITION: absolute; TOP: 195px"
runat="server"> As of Date </asp:Label>
<asp:button id="btnOK" style="Z-INDEX:
103; LEFT: 333px; POSITION: absolute; TOP: 393px"
tabIndex="18" runat="server" Width="61px"
Text="OK"></asp:button>
<asp:button id="btnCancel" style="Z-INDEX:
106; LEFT: 432px; POSITION: absolute; TOP: 393px"
tabIndex="19" runat="server" Width="61"
Text="Cancel"></asp:button>
<asp:label id="lblError" style="Z-INDEX:
101; LEFT: 32px; POSITION: absolute; TOP: 431px"
runat="server" Visible="False" Width="565px"></asp:label>
</form>

<script language="javas cript">
function ChangeColor()
{
document.forms( 0).elements
['lblError'].Hidden = 'True';
// document.forms( 0).tbDate.BackC olor
= "White";
}
</script>
</body>

Thanks,
JOhn Ritchie

Nov 17 '05 #4
Hi John,

Firstly, I want to thank Frank and Ken for their wonderful help in this
issue.

In the future, you can also try the following newsgroups for
JScript/JavaScript questions:

If you have any concerns, please feel free to let me know.

microsoft.publi c.scripting.jsc ript
microsoft.publi c.dotnet.langua ges.jscript

Best regards,

Jacob Yang
Microsoft Online Partner Support
<MCSD>
Get Secure! ¨C www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Nov 17 '05 #5
Hi Ken and all,

Thanks for the info but we long ago decided against the
built in validators. We have to do a lot of edits against
a database and decided to do all of the edit checks in a
business class tier. What we do now is the business class
checks all of the screen inputs and returns a number of
the field in error and a friendly error message to the
code behind. In the code behind I set the message into a
previously visible=false label and make it visible. I
also turn the field that has the error to a red color and
place the cursor in that field.

My problem is that when the user corrects the field in
error and re-submits the page, I again check for error but
if there are none then I continue processing the request.
This is normally not a problem but we have several
lengthly reports(1-2 minutes). Right now the user will
still see the error message while s/he waits for the
report to complete.

What I really want to do is have some javascript on the
page that senses a submit and can clear the error message
before the submit goes back to the server.

I see there is an 'onsubmit' handler but I can't seem to
get the script right for what I want to do.

Regards,
John Ritchie

-----Original Message-----
I meant "aren't" using the tools...

--
Microsoft MVPs have a question for *you*: Are you patched against the Worm?http://www.microsoft.com/security/se...ulletins/ms03- 026.asp
"Ken Cox [Microsoft MVP]" <BA************ @sympatico.ca> wrote in messagenews:u$******* *******@tk2msft ngp13.phx.gbl.. .
Hi John,

It seems to me that you are using the tools that are already available to you.You can add a regular expression validator, set it to dynamic and do what youneed without writing any code. It even switches the error message on and offusing its own Javascript.

I've put some sample code below. You may want to find a regular expression thatmore closely meets your needs here:
http://www.regxlib.com/DisplayPatterns.aspx? cattabindex=4&c ategoryId=5
Does this help?

Ken
MVP [ASP.NET]
<body>
<form id="Form1" method="post" runat="server" onsubmit="Chang eColor()"> <p>
<asp:label id="lblDate" runat="server"> As of Date </asp:label> <asp:textbox id="tbDate" tabindex="6" runat="server"width="86px" ></asp:textbox></p>
<p>
<asp:button id="btnOK" tabindex="18" runat="server" width="61px"text="OK"></asp:button>
<asp:button id="btnCancel" tabindex="19" runat="server" width="61"text="Cancel"> </asp:button></p>
<p>
<asp:regularexp ressionvalidato r id="RegularExpr essionValidator 1"runat="serve r" errormessage="P lease check the date value"
controltovalida te="tbDate"
display="Dynam ic" validationexpre ssion="^\d{1,2} \/\d{1,2} \/\d{4}">
</asp:regularexpr essionvalidator >
</p>
</form>
</body>
--
Microsoft MVPs have a question for *you*: Are you patched against the Worm?http://www.microsoft.com/security/se...ulletins/ms03- 026.asp
"John" <jc*******@dash group.com> wrote in message
news:00******* *************** ******@phx.gbl. ..
Hi,
I have an asp.net form where if the user enters an
incorect date I change the color of the textbox to red and
also display a label field with a message saying the date
is invalid. What I would like to do is to have some way to
trap the event where the user changes the date and reset
the color of the textbox to white and change the error
lablel to hidden. I want to do this before the page gets
submitted back to the server so I've tried various
Javascript routines but can't seem to get this to work.
(I'm using a date field here as example but the textbox
could be anything I ant to edit in the code-behind).

Here is the latest thing I tried, but no luck.

<body MS_POSITIONING= "GridLayout ">
<form id="Form1" method="post" runat="server"
onsubmit="Chan geColor()">

<asp:TextBox id="tbDate" style="Z-INDEX:
120; LEFT: 240px; POSITION: absolute; TOP: 191px"
tabIndex="6" runat="server" Width="86px"></asp:TextBox>
<asp:Label id="lblDate" style="Z-INDEX:
119; LEFT: 45px; POSITION: absolute; TOP: 195px"
runat="server" >As of Date </asp:Label>
<asp:button id="btnOK" style="Z-INDEX:
103; LEFT: 333px; POSITION: absolute; TOP: 393px"
tabIndex="18 " runat="server" Width="61px"
Text="OK"></asp:button>
<asp:button id="btnCancel" style="Z-INDEX:
106; LEFT: 432px; POSITION: absolute; TOP: 393px"
tabIndex="19 " runat="server" Width="61"
Text="Cancel"> </asp:button>
<asp:label id="lblError" style="Z-INDEX:
101; LEFT: 32px; POSITION: absolute; TOP: 431px"
runat="serve r" Visible="False" Width="565px"></asp:label>
</form>

<script language="javas cript">
function ChangeColor()
{
document.forms( 0).elements
['lblError'].Hidden = 'True';
// document.forms( 0).tbDate.BackC olor
= "White";
}
</script>
</body>

Thanks,
JOhn Ritchie

.

Nov 17 '05 #6

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

Similar topics

0
9721
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10633
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
10376
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...
1
10375
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9198
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6880
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
5548
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
5686
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3011
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.