Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Focus Form

Reply
Thread Tools

Focus Form

 
 
DonO
Guest
Posts: n/a
 
      10-05-2009
Not sure why this isn't working. I'm building a function to check the
value of a form field to make sure it's numeric or a comma or period.
I know this could probably be done with a regex function, but that's
not my strong suit...

The part I don't understand is when it throws the error that the field
doesn't match the criteria, it fails to put the focus on the field. It
highlights it, but focus goes to the next text box.

Thanks,
D.

<script type="text/javascript">
function check_numeric(inObj){
var allowedChars = "1234567890,.";
var checkStr = inObj;

var curChar = '';
var chkChar = '';
var isNumber = 1;

for(i=0; i<checkStr.value.length; i++){
curChar = checkStr.value.charAt(i);
for (j = 0; j < allowedChars.length; j++){
if (allowedChars.charAt(j) == curChar) break;
if (j == (allowedChars.length-1)) isNumber = 0;
}
if(isNumber == 0){
alert('Only the following characters are allowed:\n
\n'+allowedChars);
document.getElementById
(inObj.id).style.background="#f99";
inObj.focus(); // not working
break;
}
}
if(isNumber == 1){
document.getElementById(inObj.id).style.background ="#fff";
}
}
</script>
 
Reply With Quote
 
 
 
 
Asen Bozhilov
Guest
Posts: n/a
 
      10-05-2009
On Oct 5, 10:04*pm, DonO <(E-Mail Removed)> wrote:
> Not sure why this isn't working. I'm building a function to check the
> value of a form field to make sure it's numeric or a comma or period.
> I know this could probably be done with a regex function, but that's
> not my strong suit...


Something like this one?

Code:
function isNumeric(input_id)
{
var arr = document.getElementById(input_id).value.split(','),
curr;
for (var i = 0, len = arr.length; i < len; i++)
{
curr = arr[i];
if (Number(curr) != curr || /^\s*$/.test(curr))
{
return false;
}
}
return arr.length;
}
See that expression:
Number(curr) != curr

Internaly that will be looks:
Number(curr) != ToNumber(curr)
11.9.3 The Abstract Equality Comparison Algorithm in ECMA 3

The real benefit of that technique is NaN. Because comparison any NaN
value results will be false. See 8.5 The Number Type from ECMA 3

Regards.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
this.window.focus() vs. window.focus() vs. this.focus() Roger Javascript 3 03-08-2007 08:53 PM
Why "Focus and Recompose" causes back-focus issues Carl Miller Digital Photography 7 03-10-2005 12:14 PM
Fuji EXIF: Focus warning: "Out of focus" Hasan Diwan Digital Photography 3 12-26-2004 04:13 AM
Pan Focus vs Auto Focus JethroUK© Digital Photography 2 08-11-2004 06:26 PM
any manual focus cameras easy to focus? Catherine Jo Morgan Digital Photography 6 11-05-2003 06:36 PM



Advertisments