473,890 Members | 1,400 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem with Focus... Help needed.

Hi

I have created function to check date and time. at the time of
execution, if date is left empty the function returns the error
message but then the focus goes to next field. Next filed is for time
and there is also a check on that. If time is empty return error
message. Again the explorer returns error message and then shifts the
focus to date field, again error message and focus goes to time and
this story goes on and on, untill browser is killed. can somebody help
me in debuggin this script.

Thanks a lot

Nitin

<head>
</HEAD>
<body>
<script language=javasc ript>
function CheckDate(sDate ) {
var dateArray = sDate.value.spl it("/");
if (!/^\d{2}\/\d{2}\/\d{2}$/.test(sDate.val ue)) {
alert("Please enter a date in this format: dd/mm/yy.");
sDate.focus();
return false;
}
if (sDate.value.le ngth < 8 )
{
alert("Date in not valid format");
sDate.focus();
return false;
}
var now= new Date();
var longYear= now.getYear();
longYear= (longYear+"").s ubstring(2,4);
if (dateArray[2] < longYear) {
alert("Year "+dateArray[2]+" is wrong.");
sDate.focus();
return false;
}
if (dateArray[1] > 12 || dateArray[1] < 1) {
alert("Month "+dateArray[1]+" is wrong.");
sDate.focus();
return false;
}
if (dateArray[1] == 1 || dateArray[1] == 3 || dateArray[1] == 5 ||
dateArray[1] == 9 || dateArray[1] == 11){
if (dateArray[0] > 30 || dateArray[0] < 1){
alert("Date "+dateArray[0]+" in Month of "+dateArray[1]+" is
wrong");
sDate.focus();
return (false);
}
}
if (dateArray[1] == 4 || dateArray[1] == 6 || dateArray[1] == 7 ||
dateArray[1] == 8||dateArray[1] == 10||dateArray[1] == 12){
if (dateArray[0] > 31 || dateArray[0] < 1) {
alert("Date "+dateArray[0]+" in Month of "+dateArray[1]+" is
wrong");
sDate.focus();
return (false);
}
}
if (dateArray[1] == 2 && dateArray[0] > 29 || dateArray[0] < 1) {
alert("Date "+dateArray[0]+" in Month of "+dateArray[1]+" is
wrong.");
sDate.focus();
return (false);
}
return (true);
}
function CheckTime(sTime ){
if (!/^([01]?[0-9]|[2][0-3])([:][0-5][0-9])?$/.test(sTime.val ue))
{
alert("Please enter the Time in this format:\n\n
hh:mm.");
sTime.focus();
return (false);
}
var timeArray = sTime.value.spl it(":");
if (sTime.value.le ngth < 5 )
{
alert("Time in not valid format");
sTime.focus();
return (false);
}
if (sTime[0] > 23) {
alert(" hrs is not possible in a day. Invalid time.");
sTime.focus();
return (false);
}
if (sTime[1] > 60) {
alert ("mins is not possible in an hour. Invalid time.");
sTime.focus();
return (false);
}
return (true);
}
</script><HR><FOR M METHOD="POST"
ACTION="/sso/cgi-bin/g100/g100_input_form .cgi/confirm"
ENCTYPE="applic ation/x-www-form-urlencoded" NAME="input">
<TABLE CELLSPACING="0" BORDER="0" CELLPADDING="5" WIDTH="100%"><T R
ALIGN="left"><T D>Requestor</TD> <TD>Nitin Thakur</TD></TR>
<TR ALIGN="left"><T D>Email</TD> <TD>ni********* *@ubs.com</TD></TR>
<TR ALIGN="left"><T D>Date (GMT) (dd/mm/yy)</TD> <TD><INPUT TYPE="text"
NAME="startdate " VALUE="" SIZE=8 MAXLENGTH=8 ONBLUR="return
CheckDate(this) "></TD></TR>
<TR ALIGN="left"><T D>Time (GMT) 24 hrs format (hh:mm)</TD> <TD><INPUT
TYPE="text" NAME="starttime " VALUE="" SIZE=5 MAXLENGHT="5"
ONBLUR="return CheckTime(this) "></TD></TR>
<BR>
<TABLE CELLSPACING="0" BORDER="0" CELLPADDING="5" WIDTH="50%"><TR
ALIGN="center"> <TD>
<INPUT TYPE="submit" NAME="confirm" VALUE="Submit"
</FORM></BODY></HTML>
Jul 23 '05 #1
4 2227
On 16 Aug 2004 08:51:56 -0700, Nitin <ni**********@u bs.com> wrote:
I have created function to check date and time. at the time of
execution, if date is left empty the function returns the error
message but then the focus goes to next field. Next filed is for time
and there is also a check on that. If time is empty return error
message. Again the explorer returns error message and then shifts the
focus to date field, again error message and focus goes to time and
this story goes on and on, untill browser is killed. can somebody help
me in debuggin this script.
I don't believe that there's a problem with your script (I haven't taken a
close look), I think it's your HTML.

[snip]
<INPUT TYPE="text" NAME="startdate " VALUE="" SIZE=8 MAXLENGTH=8
ONBLUR="return CheckDate(this) "> [...]


The blur event is fired *every* time the control loses focus. So:

1) Click or tab to next control (B)
2) Current control (A) loses focus (event fires) and control B gains focus.
3) Validation fails, alert shows error message and user dismisses it.
4) Control A requests focus.
5) Control B loses focus and control A gains focus.
6) Validation for B fails, alert shows, and user dismisses it.
7) B requests focus.
8) Repeat from 3.

The solution is to use the change event; the event will only fire if the
value has actually changed. The user can skip a field without changing it,
and you won't end up in a loop. Of course, this means that you'll need to
validate the form at submission in case the user never enters a value.

Hope that helps,
Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail
Jul 23 '05 #2
Michael Winter wrote:
Nitin wrote:

<snip>
<INPUT TYPE="text" NAME="startdate " VALUE="" SIZE=8
MAXLENGTH=8 ONBLUR="return CheckDate(this) "> [...]


The blur event is fired *every* time the control loses focus. So:

1) Click or tab to next control (B)
2) Current control (A) loses focus (event fires) and control B
gains focus. 3) Validation fails, alert shows error message
and user dismisses it.
4) Control A requests focus.
5) Control B loses focus and control A gains focus.
6) Validation for B fails, alert shows, and user dismisses it.
7) B requests focus.
8) Repeat from 3.

<snip>

The same never ending loop is the consequence but in practice when an
alert is put up the focus goes to the OK button on the alert, so control
B loses focus at that point. Assuming that it has actually been passed
focus at that point. I have a recollection of a browser where the onblur
code is executed prior to the focusing of the next control, so when the
onblur code re-focuses the first control the browser then goes on to do
what it was planning anyway and focuses the next control, re-blurring
the first.

I too would recommend not attempting validation with onblur events.

Richard.
Jul 23 '05 #3
On Mon, 16 Aug 2004 18:28:31 +0100, Richard Cornford
<Ri*****@litote s.demon.co.uk> wrote:

[snip]
The same never ending loop is the consequence but in practice when an
alert is put up the focus goes to the OK button on the alert, so control
B loses focus at that point. [...]
I assumed that it would, but it doesn't appear to fire an event. At least
not in my tests with Opera when an event is being executed. I didn't test
with other browsers as Opera was sufficient for building the sequence I
wrote.
I too would recommend not attempting validation with onblur events.


That's more important, anyway.

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail
Jul 23 '05 #4
JRS: In article <ff************ **************@ posting.google. com>,
dated Mon, 16 Aug 2004 08:51:56, seen in news:comp.lang. javascript,
Nitin <ni**********@u bs.com> posted :

I have created function to check date and time.


One presumes that you are paid per yard of code; a much shorter date
(and time) validator can be found via the FAQ : see below.

Also see the recent thread here "Multiple Datepicker".

You can I think solve your perceived problem by validating onChange
rather than onBlur; but it would be better IMHO to validate when the
user signals that data entry is completed.

When posting code to News, it is your duty not to allow the system to
wrap your code lines - anyway, you indent in units which are too large
for convenience.

HTML should also be indented to show structure, if people are to be
expected to read it.

You seem to ask for the time in GMT; the date also should be in GMT;
then, for consistency, var longYear= now.getYear();
should probably use getUTCyear()
--
© John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.c om/faq/> JL/RC: FAQ of news:comp.lang. javascript
<URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 23 '05 #5

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

Similar topics

20
12321
by: Arne | last post by:
During testing <div style="overflow:auto;"> in CSS I noticed the mousewheel would work in Mozilla only after I made a <a href="#">some text</a> link and clicked on that, within the div. It appears as if Mozilla needs to have the focus set on that div in order for the mousewheel to work. That's all that link does. The mousewheel works perfectly in IE without the link. It scrolls the div even if there is a scrollbar on the page. Is...
31
2866
by: Benno Bös | last post by:
If I use the following construct in the frame "main" for a link to an extern site: <A HREF="http://www.any.xy" TARGET="extern"> the Browser is creating the window "extern", loading the page www.any.xy an setting the focus to "extern". But when I go return to "main" without closing "extern", a click to an other link (e.g. www.2nd_any.xy) on "main" does not setting the focus to "extern".
6
4858
by: MickG | last post by:
Hi, I am trying to validate these values, this seems to work fine for the phone number and name but I am trying to get the program to fail to submit and set the focus on the date when 2006 is selected and submitted. Thanks in advance for any help.
7
1421
by: Ray | last post by:
Hi all, I'm new to JavaScript and am trying to create a client side JavaScript form validation script. I'm doing ok with validating text input boxes but have a problem that I have not been able to solve and one I hope someone will help me resolve. I want to check the contents of a drop down box and if a certain item is selected, check the contents of a text input box to see if a any text was entered. If no text is entered, I want to...
6
1708
by: nizar.jouini | last post by:
I have web page that contains two links. link "a" and link "b". When I click on "a" a small window should pop up. when I click on "b" another small window should pop up. So what you should see now is the base window which contains links "a" and "b" and two small pop-up windows "a" and "b". Now I can make the script to pop up "a" and "b" but the problem is that they're not shown at the same time. Any suggestions????
13
6272
by: ldan | last post by:
Hi everybody, I would not consider myself an expert in javascript - but so far whatever I know, helped me reaching my goals. Recently I started to experience a lot of javascript errors related to opening up a popup window in an application I wrote. The error messages are quite diverse: "Object doesn't support this property or method" or "Unspecified error." or "The callee (server ) is not available and disappeared; all connections are...
3
4628
by: Deidre | last post by:
I have a page with a drop down and several web controls. The drop down is set to postback and it reloads the textboxes based on the selected drop down item. I set the focus to the first textbox control on the client like this: <script language="vbscript"> Option Explicit ....other code If Not <%=mbReadOnly%> Then document.frmDetails.txtName.focus() End If
6
4489
by: Niksa Baldun | last post by:
Hi, I am trying to capture data from magnetic stripe reader which is connected via keyboard interface (therefore indistinguishable from normal keyboard input). Basically, I am doing the following (in the Form.Keypress event handler): strCardData &= e.KeyChar e.Handled = True
13
2289
by: Richard Shewmaker | last post by:
I've only recently returned to (trying to) doing simple JS, so this is probably a really lame question. I have a Web page with a series of graphics in it (window A). Clicking on a graphic in A opens a small pop-up window (window B) over A. If the visitor clicks on B, it closes. If the visitor clicks back on A -- the page or a new graphic -- A comes
0
9978
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
11222
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
10810
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
10460
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
8015
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
5845
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
6041
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4270
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3275
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.