473,385 Members | 1,942 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,385 software developers and data experts.

Tab repeats function 6 times

Hello Everyone!

I am trying to implement a form which some of the fields I have are
required.

In order to do that I am using the onBlur function, so when I either
click on a different field, or Tab out to the next field, it should
check if my last entry was not blank, and in case it was, it should
return to that field until the user enters a valid entry.

Here is my code:

-------------------------------------------------------------------------------------
<SCRIPT LANGUAGE="JavaScript">
<!--
function checkInput(element,curentField) {
if (element.value == "" ) {
document.form1[curentField].focus();
alert("All fields with * are required!");
}
}
// -->
</SCRIPT>
<form name="form1" method="post" action="">
<INPUT TYPE="text" NAME="Name" VALUE="" size="17"
onBlur="checkInput(this,'Name')">
<input type="submit" name="Submit" value="Submit">
</form>
-------------------------------------------------------------------------------------

The problem I am having is, when I use the Tab to move to the next
field, the alert message pops up 6 times. If I click on some other
field or on the background using the mouse, then the alert message
shows up only once, like it should!

Any idea why the Tab causes 6 alerts and how can I do to change that?

Thanks,

Thiago

Oct 11 '06 #1
1 1216
Thiago Jorge wrote:
Hello Everyone!

I am trying to implement a form which some of the fields I have are
required.

In order to do that I am using the onBlur function, so when I either
click on a different field, or Tab out to the next field, it should
check if my last entry was not blank, and in case it was, it should
return to that field until the user enters a valid entry.
That is a rather unfriendly feature. By all means alert the user to
invalid input, but use an in-page error message and do not force them
back to the input. They may know that their input is invalid but wish
to do something else before correcting it.

Here is my code:

-------------------------------------------------------------------------------------
<SCRIPT LANGUAGE="JavaScript">
The language attribute is deprecated, type is required.

<script type="text/javascript">
<!--
Do not use HTML comment delimiters inside script elements. They serve
no useful purpose and may cause problems.

function checkInput(element,curentField) {
if (element.value == "" ) {
document.form1[curentField].focus();
You already have a reference to the element, you could just use:

element.focus();

But...
alert("All fields with * are required!");
you have a timing issue: you put focus on the element, then call alert
so the input to loses focus, so the onblur event fires, which calls the
alert again, which fires onblur, and so on - your code causes Firefox
to stop responding. Maybe IE just gives up after half a dozen such
loops.

Using an in-page message and ditching the focus() call solves the
problem.
}
}
// -->
</SCRIPT>
<form name="form1" method="post" action="">
<INPUT TYPE="text" NAME="Name" VALUE="" size="17"
It is not a good idea to give any element a name of "Name" - as far as
the HTML specification is concerned, that is identical to "name". What
should I see when I do:

alert(document.forms['form1'].name);

onBlur="checkInput(this,'Name')">
There is no need to pass the name of the input, you already have a
reference to it with 'this', and could get it using 'this.name' or,
within the function, element.name if you wanted to.
<input type="submit" name="Submit" value="Submit">
As above, giving a submit button a name of submit is a bad idea, there
is no need for it to have a name at all.
--
Rob

Oct 12 '06 #2

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

Similar topics

3
by: kj12345 | last post by:
I am writing a content management system where users can remove and edit data. I have a datagrid with a 'remove' button on each row. If a user clicks this (or 'add new', or 'edit') it works fine....
4
by: viuxrluxvbbc | last post by:
Hi im trying to write a program that will read in numbers and display them in ascending order along with a count of how many times it repeats. i got the numerical order portion done but cant figure...
28
by: Larax | last post by:
Best explanation of my question will be an example, look below at this simple function: function SetEventHandler(element) { // some operations on element element.onclick = function(event) {
12
by: pob | last post by:
At the end of a procedure I have the below: Exit_Proc: Exit Sub Err_Proc: Call ErrorLog(user,error type, filename) resume exit_Proc End Function The odd thing is if there is a single error...
27
by: Neil | last post by:
Hello all! I wrote program with a array of pointers, and I suspect they are pointing at each other in the Do ...While loop. Something is messed up with the increment variable word. A program...
6
by: xian2 | last post by:
Hi All, I have created a query with 6 related tables as the record source. The query includes 4 fields from 4 of the tables: tblTourBookings TourStartDate tblAdditionalCosts
7
thatos
by: thatos | last post by:
need help I was trying to do a program which could eliminate numbers which repeats themselves in a list the program below works but it gives me an error can you please try to find the problem as ...
5
by: andi | last post by:
Hello, I created a report in Acess2000 and now it repeats itself three times. At first I thought its a problem with the page margins but that would mean that empty or almost empty pages would...
5
by: mad.scientist.jr | last post by:
According to http://www.quirksmode.org/js/forms.html you need to look through a radio button's members to find the value: I tried writing a reusable function to return the value, see code at:...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
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...

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.