OnFocus

A

Andy

Hi

I am validating myform fields in an i loop. I have 20
fields named f1 to f20. I am checking the value of each
using eval('myform.f' + i + '.value').

When I find an error, I want to put out an alert and set
focus on the erroneous field. How do I set focus on this
field if I only know the i value?

Something like ('myform.f' + i + '.onfocus())

The icing on the cake ... can I change the bgcolor of the
field to red as well?
 
J

Jon Spivey

Hi,
I'd address the field thru the form elements collection
then you don't need to worry about the name (you might
start i at greater than 0 if f1 isnt the first field in
your form) something like
<script type="text/javascript">
function checkForm(f){
for(i=0;i<21;i++){
if(f.elements.value!='whatever'){
alert('bad data');
f.elements.focus();
return false;
}}
return true;
}
</script>
<form onsubmit="return checkForm(this)">

Jon
Microsoft MVP - FP
 
A

Andy

Thanks very much Jon - how about the bgcolor? Same
function?
-----Original Message-----
Hi,
I'd address the field thru the form elements collection
then you don't need to worry about the name (you might
start i at greater than 0 if f1 isnt the first field in
your form) something like
<script type="text/javascript">
function checkForm(f){
for(i=0;i<21;i++){
if(f.elements.value!='whatever'){
alert('bad data');
f.elements.focus();
return false;
}}
return true;
}
</script>
<form onsubmit="return checkForm(this)">

Jon
Microsoft MVP - FP
-----Original Message-----
Hi

I am validating myform fields in an i loop. I have 20
fields named f1 to f20. I am checking the value of each
using eval('myform.f' + i + '.value').

When I find an error, I want to put out an alert and set
focus on the erroneous field. How do I set focus on this
field if I only know the i value?

Something like ('myform.f' + i + '.onfocus())

The icing on the cake ... can I change the bgcolor of the
field to red as well?
.
.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top