JavaScript - Numeric Validation
Don't you just love trying to modify code in a language you don't actually know?
Basically, I just need to ensure that there is at least one digit in the input string from a field. Here's my first attempt:
var validSN = false;
for( var i = 0; i <= f.sn.value.length; i++ )
if( f.sn.value[i] >= '0' && f.sn.value[i] <= '9' )
validSN = true
if (validSN != true )
{
alert('A valid S/N must have numbers. If you do not have a valid S/N please leave blank.');
f.sn.focus();
return false;
}
The problem is that it never passes the check. Any ideas why?
[718 byte] By [
Telos] at [2007-11-11 10:22:57]

# 1 Re: JavaScript - Numeric Validation
Here's the loop you need:
var txtObj = document.getElementsByTagName('input');
function findNumber() {
for(var i=0;i<txtObj.length;i++){
for(var x=0;x<=9;x++) {
if(txtObj[i].value.indexOf('"'+x+'"')!= -1;
}}}
that should get you going in the right direction.
JPnyc at 2007-11-11 23:34:31 >

# 4 Re: JavaScript - Numeric Validation
I basically cut/pasted your code and modified the variable names to match:
var validSN = false;
var txtObj = document.getElementsByTagName('sn');
for(var i=0;i<txtObj.length;i++)
{
for(var x=0;x<=9;x++)
{
if(txtObj[i].value.indexOf('"'+x+'"')!= -1)
{
validSN = true;
}
}
}
I also tried a regular expression version.
var reg = new RegExp( '.*\d.*');
if ( !reg.exec(f.sn.value) && f.sn.value.length != 0)
{
alert('A valid S/N must have numbers. If you do not have a valid S/N please leave blank.' + reg.exec(f.sn.value) + ' ' + f.sn.value);
f.sn.focus();
return false;
}
I added the result of the regular expression and the field itself to the alert out of curiousity. The regexp is returning null, but the value is definately showing up correctly. I think javascript just doesn't like me... :(
EDIT: It occurs to me that you don't know where f is defined:
var f=window.document.callinfo;
At the top, of course.
Telos at 2007-11-11 23:37:26 >

# 5 Re: JavaScript - Numeric Validation
Well that's why it won't work. There are no tags 'sn'. That function returns an object of all tags of a particular type, e.g. anchor tags, image tags, input tags, etc.
Also I think you need to include a break; statement in the true, or else return true; so the loop doesn't continue.
How many text boxes is it supposed to check? If there's just one we don't need to make a txtbox object.
JPnyc at 2007-11-11 23:38:30 >

# 6 Re: JavaScript - Numeric Validation
Just one input tag, and it is there... I just didn't include all the html. I didn't create an object with the regexp method, just used the f.sn.value directly and it still doesn't work, but does display the value in the alert so we know it's referencing the field correctly.
Telos at 2007-11-11 23:39:35 >

# 7 Re: JavaScript - Numeric Validation
Ah ok, now we're cookin'. So there's just one input tag, that makes it simpler. Give the input an id (not a name, an id) then use:
var txtObj=document.getElementById('myID');
Now we should be getting closer
JPnyc at 2007-11-11 23:40:28 >
