473,549 Members | 2,583 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2175
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.leng th < lgth) {
alert('Not enough characters in "' + str.value + '".\nThe length is
' + str.value.lengt h + '. 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="minLen gth(document.fo rm1.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.leng th < lgth) {
alert('Not enough characters in "' + str.value + '".\nThe length is
' + str.value.lengt h + '. 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.lengt h + '. 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="minLen gth(Info,2)"

Full working example below:

<html><head><ti tle> form play </title>
<script type="text/javascript">
function minLength(str, lgth) {
if (str.value.leng th < lgth) {
alert('Not enough characters.\nTh e length is '
+ str.value.lengt h + '. 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="minLen gth(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.n et.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="minLen gth(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
2013
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 value from the selection fields. Text inputs work just fine. Below is the form and validation script (I've added "...." to indicate additional...
6
5803
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). The script works on IE and Mozilla but fails on Safari on the MacOSX. Here is my code.. // ok files with proper extension var reOKFiles =...
34
3806
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
17072
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 alignments and div widths are off. It's as if firefox has a different definition of a pixel than safari. Here is the url:...
2
2104
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. Using Safari,my first iteration the script works fine ( indicating that there are 33 form variables ). When trying another dropdown select value, the
10
5509
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 pressed, submit the form - bypassing my onclick handler. I can partially fix this with: <form onsubmit='return false;'>
2
3816
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 some keyboard events -ie. Event.observe(document,'keyup',function (e) { // code here });. The problem is that when somebody clicks a button...
3
17911
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 background). In IE it works fine but in firefox and safari it doesn't focus on the original page. I have looked on all sorts of forums etc and apparently...
0
7459
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...
0
7726
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. ...
0
7967
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...
0
7819
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...
1
5377
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...
0
3505
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...
1
1953
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
1
1064
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
772
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...

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.