Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

???OnBlur and .focus help??? 1

Status
Not open for further replies.

toddOne

Programmer
Jul 20, 2001
41
0
0
US
I am currently doing validation checks for most every text box in my form, but b/c part of the check is whether or not the field is empty i am having a problem with onblur. if a textbox is blank and the user tabs to the next textbox i receive the desired result (alert box), but b/c the there is a validation check on the field that was next in the tab order i now receive an additional, undesired alert box b/c that field is blank....creating an endless loop of alert boxes.

any suggestions would be great!

<script language=&quot;JavaScript&quot; type = &quot;text/javascript&quot;>
var whitespace = &quot; \t\n\r&quot;;
function fieldValidate(fld, typ, desc)
{
var checkToMake = typ;
var field = fld;
var d = desc;

switch (checkToMake)
{
case 'notblank': if (isEmpty(field.value))
{
alert(d + &quot; may not be empty&quot;);
field.focus();
}
break;
case 'isnumber': if(!isInteger(field, desc))
break;
}
}
function isEmpty(s)
{
var i;
if ((s==null) || (s.length ==0))
return true;
// Search string looking for characters that are not whitespace
for (i=0; i < s.length; i++)
{
var c = s.charAt(i);
if (whitespace.indexOf(c) == -1)
return false;
}
// All characters are whitespace.
return true;
}
function isDigit(c)
{
return ((c >= &quot;0&quot;) && (c <= &quot;9&quot;) || (c == &quot;.&quot;))
}
function isInteger(field, desc)
{
var i, c;
var s = field.value;
var d = desc

if (isEmpty(s))
{
alert(d + &quot; cannot be empty&quot;);
field.focus();
return false;
}
for (i=0; i<s.length; i++)
{
//Check if all caharacters are numbers
c = s.charAt(i);
if (!isDigit(c))
{
alert(d + &quot; must contain only digits&quot;);
field.focus();
return false;
}
}
return true;
}
</script>

example call of fieldValidate:
Login: <input type=&quot;text&quot; name=&quot;txtBox&quot; id=&quot;txtBox&quot; size=&quot;30&quot; onblur=&quot;fieldValidate(txtBox, 'isnumber', 'Login')&quot;>

....thanks in advance for any suggestions!

 
I have one solution for you. I have an old web site I haven't updated for a while now.

Go to :
and click on the keez link on the right.

The code checks that the keys entered is part of a set of allowed keys. You wouldn't need to do onblur. This example works with NS4-NS6 and IE4+ if my memory serves me right. The code is ugly but grab it and see how it works. Adapt it to your sauce and see if you like it.

There are about a thousand different ways to do do form validation, none of them are perfect and all have inconveniences. But looking at different ways of implementing it is a good way of knowing which one fits your demand better.

Hope this helps. Gary Haran
 
You could also just check each field when the form is submitted rather than using the onBlur event. Mighty :)
 
thank you xutopia...i will give your code a shot.

mighty thanks for the reply but i already have a check onSubmit, but would prefer to prevent invalid data as it is enetered.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top