473,325 Members | 2,771 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,325 software developers and data experts.

NN and Safari won't return focus to the same input

I want to validate the data entered into a form text field by calling a
function with onblur, doing the validation and, if the input is invalid,
giving an alert to the user and returning focus and select to the invalid
field to force the user to correct the entry.

A simple example is at www.brilley.co.uk/TestInfo.htm

The problem is that this example works in IE but not in NN or Safari, which
do not return focus or select back to the text field.

I understand that many think it is not good practice to force the user back
to the field so that she has to correct it before she can progress, but I
think the application I have to design would be best served by this
approach.

Clearly, the way I have coded it is either in error, or that's how NN and
Safari are designed. You can focus on another field but not on the one that
called the event, it seems.

I'd very much appreciate any help on either fixing my code or another way of
producing the same result. In the real application it's not practical to
validate on submit because there are many validations on the same page,
which I think leads to confusion. Better to validate each field one at a
time.

Thanking you in anticipation.
Jul 23 '05 #1
4 2158
Roger Withnell wrote:
I want to validate the data entered into a form text field by calling a
function with onblur, doing the validation and, if the input is invalid,
giving an alert to the user and returning focus and select to the invalid
field to force the user to correct the entry.

A simple example is at www.brilley.co.uk/TestInfo.htm

The problem is that this example works in IE but not in NN or Safari, which
do not return focus or select back to the text field.

<snip>

I have no idea why this is, but if you call the function that sets the
focus from somewhere other than the textbox onblur it works (at least on
my Netscape 7.2, IE 6, Firefox 1.0PR. Maybe you'd consider checking it
when the form is submitted. There's probably a cleaner way to write this
but this shows what I mean:

function minLength(str, lgth){
if (str.value.length < lgth) {
alert('Not enough characters in "' + str.value + '".\nThe length is
' + str.value.length + '. Minimum allowed is ' + lgth + '.');
document.forms['form1'].elements[str.name].focus();
document.forms['form1'].elements[str.name].select();
return false;
}
}

<body>
<form name="form1" method="post" action="">
Enter Info <input name="Info" type="text" id="Info" size="4"
maxlength="4">
(2-4 char's)
<br><br><br><br>
<input type="button" onclick="minLength(document.form1.Info,2)"
value="checkit">
</form>
</body>
</html>

Mike
Jul 23 '05 #2
mscir wrote:
Roger Withnell wrote:
I want to validate the data entered into a form text field by calling a
function with onblur, doing the validation and, if the input is invalid,
[snip] I have no idea why this is, but if you call the function that sets the
focus from somewhere other than the textbox onblur it works (at least on
my Netscape 7.2, IE 6, Firefox 1.0PR. Maybe you'd consider checking it
when the form is submitted. There's probably a cleaner way to write this
but this shows what I mean:

function minLength(str, lgth){
if (str.value.length < lgth) {
alert('Not enough characters in "' + str.value + '".\nThe length is
' + str.value.length + '. Minimum allowed is ' + lgth + '.');
Long strings can be broken between the quoted bits, e.g.

alert('Not enough characters in "'
+ str.value + '".\nThe length is '
+ str.value.length + '. Minimum allowed is '
+ lgth + '.');

Just remember to put the returns just before the '+' signs and all
will be fine.
document.forms['form1'].elements[str.name].focus();


Have a think what you are doing here. The bit:

document.forms['form1'].elements[str.name]

returns a reference to ... *str*. You already have the reference and
have used it above. So just use:

str.focus();

Similarly for the following line, it becomes:

str.select();

Lastly, you are using a button inside the form to submit the reference,
so you don't need the document.form1 bit, just the name so the
onclick becomes:

onclick="minLength(Info,2)"

Full working example below:

<html><head><title> form play </title>
<script type="text/javascript">
function minLength(str, lgth) {
if (str.value.length < lgth) {
alert('Not enough characters.\nThe length is '
+ str.value.length + '. Minimum allowed is '
+ lgth + '.');
str.focus();
str.select();
return false;
}
}
</script>
</head>
<body>
<form name="form1" method="post" action="">
<p>Enter Info</p>
<input name="Info" type="text" id="Info" size="4"
maxlength="4">
(2-4 char's)
<br><br><br><br>
<input type="button" value="checkit"
onclick="minLength(Info,2)">
</form>
</body>
</html>

Jul 23 '05 #3
Fred Oz wrote:
<snip>

Those are all good points, and definitely clearer than my way.
Jul 23 '05 #4
On Mon, 18 Oct 2004 19:23:13 +1000, Fred Oz <oz****@iinet.net.auau> wrote:

[snip]
Lastly, you are using a button inside the form to submit the
reference, so you don't need the document.form1 bit, just the name
so the onclick becomes:

onclick="minLength(Info,2)"


I wouldn't necessarily recommend that. It makes the assumption that the
user agent creates a custom scope chain that includes the form, and there
is no reason to believe that to be true.

minLength(this.form.elements['Info'],2)

whilst more verbose (potentially a good thing in itself), would be much
more reliable.

[snip]

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #5

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

Similar topics

7
by: | last post by:
I can't get IE 6 to read the values in my <SELECT..> data entry fields. Netscape 7 and Opera see them, and IE will pass the values to the database, but the javascript validation script gets a null...
6
by: Ouch! | last post by:
I am using Regular Expressions and Javascript to validate a form, specifically I want to make sure that if they try to upload a file that it has a proper name w/ certain extensions (doc,pdf, rtf). ...
34
by: Simon Wigzell | last post by:
document...focus() will scroll the form to move the specified text field into view on everything I have tried it with except Safari on the MAC. The form doesn't move. Any work around? Thanks.
2
by: cbjewelz | last post by:
Hey all. So I'm having problems with cross browser alignments. I'm looking at Safari and Mozilla Firefox. I develop in Safari and so it looks perfect there however in Firefox my vertical...
2
by: justplain.kzn | last post by:
Hi, I have a table with dynamic html that contains drop down select lists and readonly text boxes. Dynamic calculations are done on change of a value in one of the drop down select lists. ...
10
by: Tim Streater | last post by:
I have a form and a button to submit it. The button is made from: <input type=button onclick='myHandler(this.form);'> This all works fine except that in Safari 2.0.4, the enter/return keys, if...
2
by: tonydspaniard | last post by:
Hi there all, Using prototype.js I render a flash movie within a DIV dynamically and then I try to attach some events in order to maintain focus on main window cuz is responsible of event handling...
3
by: acehigh1983 | last post by:
Is it possible to focus a window in firefox and safari?? i have a webpage that opens another window, it then focuses on the original window (the new window that opened now behind it in the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.