473,473 Members | 1,813 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How to Change Focus to given Form Field

Hi,

I have an HTML page with a FORM and some input fields.
On the fields I wish to do validation as the punters change the field
values.

If they get it wrong, then I tell them and then wish to put
the focus back to the offending field.

( It works if a use an 'onblur' event but not an 'onchange' )
What mean is that the changing focus happens.
The 'onblur' is a pain because if a minimise the window and then bring
it back up, the validation function is called for each box during the
loading.
In practice I may have a form with up to 60 boxes and this would be a bit
of a drag.

eg:
Try the following file and enter an invalid number in either of the
number boxes then use the mouse ( or TAB key )
to move to a new box.
<html>
<head>
<link rel='stylesheet' href='/ripple-order/common/rip.css' />
<title>Submit a Ripple Order</title>

<script language="Javascript">

function validate_generic_numberField( dataRootDetails )
{
alert( "Entered 'validate_generic_numberField' field_name=["
+ dataRootDetails.name + "]" );
// expect dataRootDetails to be either the value of
// document.productDetails or document.serviceDetails
var v = dataRootDetails.value;
var fname = dataRootDetails.name;

var err = 0;
var errtext = "";
if( v.search(/[^0-9\.]/g) != -1 )
{ err = 1;
errtext = "Problem with field [" + fname + "]. \n\nValid chars
for a number field are :\n '0', '1', .., '9' and '.', '+', '-'\n";
}

if( err )
{
alert( errtext );
dataRootDetails.focus();
}
}
</script>
</head>
<body bgcolor="#FFFFFF">

<form>
<table>
<tr>
<td valign="top" class="fdesc">A text field in here</td>
<td><textarea name="TextField1" rows="4"
cols="40"></textarea></td>
<td valign="top" class="ftype">Text</td>
</tr>

<tr>
<td valign="top" class="fdesc">A string field in here</td>
<td><input type="text" name="StringField1" value=""></td>
<td valign="top" class="ftype">String</td>
</tr>
<tr bgcolor="cyan">
<td valign="top" class="fdesc">Try alpha char here then move to
another box ( onblur event )</td>
<Comment>Note. Using the onblur event because the onchange event does not
seem to allow the focus to be re-set back offending field
</Comment>
<td><input type="text" name="numberField1"
onblur="validate_generic_numberField(this)" value="">
</td>

<td valign="top" class="ftype">Number [ ]</td>
</tr>
<tr>
<td valign="top" class="fdesc">Try alpha char here then move to
another box ( onchange event )</td>
<Comment>Note. Using the onblur event because the onchange event does not
seem to allow the focus to be re-set back offending field
</Comment>
<td><input type="text" name="numberField2"
onchange="validate_generic_numberField(this)" value="">
</td>

<td valign="top" class="ftype">Number [ ]</td>
</tr>

</table>
</body>
</html>
Jul 20 '05 #1
2 14249
fish wrote:
On the fields I wish to do validation as the punters change the field
values. If they get it wrong, then I tell them and then wish to put
the focus back to the offending field.


I strongly suggest you reconsider. If you try this you'll find a lot of
people type something in to the field, hit tab, then start typing what they
want in the next field. If you hijack the cursor back to the previous field
they'll type over whatever they put.

This means they waste time typing in the wrong box (lots of time if they
don't notice!) and find it much harder to edit whatever they had initially
(e.g. if it was only a minor error, but the zapping forces them to retype
the entire field).

--
David Dorward http://david.us-lot.org/
Redesign in progress: http://stone.thecoreworlds.net/
Microsoft announces IE is dead (so upgrade):
http://minutillo.com/steve/weblog/20...ces-ie-is-dead
Jul 20 '05 #2
On 03.7.10 9:35 AM, fish wrote:
I have an HTML page with a FORM and some input fields.
On the fields I wish to do validation as the punters change the field
values.

If they get it wrong, then I tell them and then wish to put
the focus back to the offending field.


Would you like it if your bank manager asked you to fill in a form and then
looked over your shoulder the whole time pointing out every tiny mistake you
made?

Of course not.

Just wait until the user presses the submit button.

Phil
--
Philip Ronan
ph***********@virgin.net
(Please remove the "z"s if replying by email)
Jul 20 '05 #3

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

Similar topics

5
by: 'bonehead | last post by:
Greetings, I'm still something of a newbie to html/php/mysql. I have a php/html form with several fields of type "input". If the user enters improper data in a particular field and clicks the...
6
by: Rey | last post by:
Howdy, all. Appreciate your help. Have a one to many relation between a client and visit table. In the visit subform, I have a visittype and counselor field which are comboboxes. If I set...
1
by: tdmailbox | last post by:
Is there a vb command that can tell me if I have focus in the child or parent form? Basicly I have a search macro that needs me to have focus to any field in the parent form. If my focus is set...
3
by: Philip Townsend | last post by:
I have an aspx page that contains 2 user controls, each containing a seperate textbox and button. I would like to specify that one of the buttons recieve focus when the page loads. Also, I would...
3
by: Jim in Arizona | last post by:
I have a gridview that's being populated from an access db query. The problem I'm having is that the date/time fields in access that are populating the gridview are showing both date and time, when...
2
by: sreemati | last post by:
Hi One of the web form has 3 dates fields: Date of form entry, date of diagnosis and date of death. Date of form entry has future date validation done which I thought of extending to the...
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
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,...
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...
0
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...
0
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,...
1
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...

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.