473,804 Members | 2,116 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

keeping focus on a text field

Hello, Assuming I have the functions, "isNumber" and "isEmpty", how
would I write the HTML INPUT type="text" element such that a person
cannot exit the element unless they have typed in a valid number (as
defined by the function isNumber) or left the field empty (as defined
by isEmpty)? Thus, if they have typed in "aaa" and then pressed "Tab"
to go the next element, they'd get a warning message, and be returned
to the old element?

Ideally, this solution would work for both the latest versions of
Firefox and IE.

Thanks, - Dave

May 29 '06 #1
6 13817
Ok step one: Keeping someone focused on a text field.

You would this it would be this simple:

<input type="text" onBlur="this.fo cus();">

but its not, but this works

<input type="text" onBlur=" var that = this; setTimeout(func tion()
{that.focus();} , 5); ">

seems to work pretty well;

as for your function that checks the input's value it might look
something like this

<input type="text" onBlur="checkMe (this);">

<script>
function checkMe (me) {
if (me.value != 12) {
alert ('ERROR you must enter 12!');
me.focus();
}
}
</script>

May 29 '06 #2
la***********@z ipmail.com said the following on 5/28/2006 8:18 PM:
Hello, Assuming I have the functions, "isNumber" and "isEmpty",
I think your isEmpty function would be redundant in this case. If it is
empty, then it can't be a number and isNumber would cover it.

how would I write the HTML INPUT type="text" element such that a person
cannot exit the element unless they have typed in a valid number (as
defined by the function isNumber) or left the field empty (as defined
by isEmpty)?
You use the onSubmit of the form to validate the entire form and notify
of errors then.

<input type="text" onchange="isNum ber(this)">

function isNumber(fieldT oValidate)
{
//check fieldToValidate .value to see if it
//is a Number or not (whatever you define "Number"
//to be.
if (!validNumber)
{
alert('This is a poorly implemented way to ' +
'keep you from going anywhere else ' +
'because I do not know better');
fieldToValidate .focus();
}
}

If you doubt the alert message, use the tab key and tab through your
form elements.
Thus, if they have typed in "aaa" and then pressed "Tab"
to go the next element, they'd get a warning message, and be returned
to the old element?
See above. But do not fall for the trap of using onblur, use the
onchange event handler.
Ideally, this solution would work for both the latest versions of
Firefox and IE.


And what about Opera, KMeleon, Camino or any other browser? The point
being, don't fall into the "two browser" trap where you think there are
only two browsers to consider.
--
Randy
comp.lang.javas cript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
May 29 '06 #3
deadlyicon said the following on 5/29/2006 12:08 AM:

Please quote what you are replying to.

If you want to post a followup via groups.google.c om, don't use the
"Reply" link at the bottom of the article. Click on "show options" at
the top of the article, then click on the "Reply" at the bottom of the
article headers.
<URL: http://www.safalra.com/special/googlegroupsreply/ >
Ok step one: Keeping someone focused on a text field.
That's annoying at best and anti-user friendly at worse.
You would this it would be this simple:

<input type="text" onBlur="this.fo cus();">
Never use the onblur to validate a field. Use the onchange instead.
but its not, but this works

<input type="text" onBlur=" var that = this; setTimeout(func tion()
{that.focus();} , 5); ">

seems to work pretty well;
Depending on your definition of "work pretty well"
as for your function that checks the input's value it might look
something like this

<input type="text" onBlur="checkMe (this);">
<input type="text" onchange="check Me(this)">
<script>
function checkMe (me) {
if (me.value != 12) {
alert ('ERROR you must enter 12!');
me.focus();
}
}
</script>


--
Randy
comp.lang.javas cript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
May 29 '06 #4
Unfortunately, neither "onChange" nor "onBlur" work. Although the
function gets called, if you clic "Tab" the focus changes to the next
text field even if you have some "focus" statement in your function.

- Dave

May 30 '06 #5
la***********@z ipmail.com said the following on 5/30/2006 10:59 AM:
Unfortunately, neither "onChange" nor "onBlur" work.
And unfortunately, you haven't learned how to quote what you are
replying to. So here it is, once again:

Please quote what you are replying to.

If you want to post a followup via groups.google.c om, don't use the
"Reply" link at the bottom of the article. Click on "show options" at
the top of the article, then click on the "Reply" at the bottom of the
article headers.

<URL: http://www.safalra.com/special/googlegroupsreply/ >

But yes, onchange and onblur do work as intended.
Although the function gets called, if you clic "Tab" the focus
changes to the next text field even if you have some "focus"
statement in your function.


Then show your code because something else in your code is wrong, not in
the basic idea of how to do what you are wanting to do.

--
Randy
comp.lang.javas cript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
May 30 '06 #6
JRS: In article <qM************ ********@comcas t.com>, dated Mon, 29 May
2006 00:30:29 remote, seen in news:comp.lang. javascript, Randy Webb
<Hi************ @aol.com> posted :
la***********@ zipmail.com said the following on 5/28/2006 8:18 PM:
Hello, Assuming I have the functions, "isNumber" and "isEmpty",


I think your isEmpty function would be redundant in this case. If it is
empty, then it can't be a number and isNumber would cover it.


(A) It would be good practice to use, rather than isEmpty, isBlank, the
latter implying visibly empty.

I was startled, once, by the strange report given by a Web page
checker - it turned out (a) that in copy'n'pasteing the URL I'd
transferred a trailing space, (b) which was not trimmed, (c) but
included in the URL requested, (d) which generated a 404 page,
(e) which was tested, (f) and (IIRC) failed to validate.
(B) But AFAIK isNumber is not standard.

It could be implemented with, say, /[+-]?\d+/, in which case Empty or
Blank would fail; but it could be implemented as

function isNumber(X) { return !isNaN(+X) }

which counts Blank as zero.
One cannot safely assume much about the behaviour of an OP's unquoted
code; and it might be considered useful in some cases for Blank to be
treated as zero..

--
© 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.
May 30 '06 #7

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

Similar topics

7
6220
by: Rick Caborn | last post by:
Does anyone know of a way to execute sql code from a dynamically built text field? Before beginning, let me state that I know this db architecture is built solely for frustration and I hope to make it better soon. Unfortunately, there is never a non-crucial time in which we can do an upgrade, so we are stuck for now. Point 1: There are multiple tables: students, courses, cross-reference
2
11064
by: ehm | last post by:
I am working on creating an editable grid (for use in adding, deleting, and editing rows back to an Oracle database). I have a JSP that posts back to a servlet, which in turns posts to a WebLogic SFSB and, from there, to the database. On the front end, all cells appear as text fields. However, for certain cells, when the user clicks on the cell, the text field turns into a drop-down field (i.e. Select object), defaulting to the value...
1
3448
by: Per Magnus L?vold | last post by:
Hi! I'm working on a JSP application, and wish to define a text-field so that it becomes marked (highligted) when it obtains focus. I've tried giving the text-field a value when it obtains focus, but the value is not marked. So if i wish to alter the value, I have to manually delete the given 'onFocus' value before I set a new value. <INPUT TYPE=TEXT NAME=test size=30 onFocus=\"this.value='I want this to be highlighted!'\">
3
6898
by: VA | last post by:
t=document.getElementById('mytable') is a HTML table with some input fields in its cells Why doesnt t.getElementsByTagName('tr').firstChild.focus; put the focus on that text field? It doesnt give any errors, the focus just doesnt change.
3
8321
by: Steve Yerkes | last post by:
There seems to be way too much confusion over how to set focus on the a field using a field validator. I looked all over the web and found people trying to do this, but not getting anywhere. There are a couple of people selling components... but that is not really an option for me... So, I took the plunge and modified the "WebUIValidation.js" file to make it happen... After tracing through file, I figure it out. It was actually pretty...
4
4668
by: sufian | last post by:
Below is the field where user enters his/her email address and the AJAX post request is sent to the server and the user sees the message: echo("<div id=\"message\" class=\"success\">Thank you! You have been successfully registered.</div>"); within 1.5 seconds. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta...
2
3551
by: creative1 | last post by:
Hi Everyone I switch between various fields on the form by using ENTER and TAB keys. I want to highlight existing value of a text fields when I get it focus. When we use combo, on getting focused on it it highlight the whole value present in it. Can I do this with text field and masked fields too? Thanks in advance.
12
11465
by: Sculder | last post by:
Hello, I'm running into an interesting issue and I wanted to verify it was a bug with Internet Explorer 7. I have a field that has an javascript onBlur event. When you set focus to the text field, it launches a custom event handler to process the existing value in that text field. Well after the onBlur function has completed, the cursor looks like it's not there, almost as if it didn't set focus to the text field you were currently on. ...
0
9716
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
9595
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
10604
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...
1
10359
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
10101
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...
0
9177
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
5675
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4314
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3837
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.