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 John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

problems with isNumeric()

Status
Not open for further replies.

emms

Programmer
Jan 11, 2001
55
GB
Hi,

I'm fairly new to javascript. I'm trying to write a script to check whether an input is numeric or not and do a different thing depending on the result

at the moment i'm trying this:

if (isNumeric(document.selector.branchid.value)){
searchlink = searchlink + "&branchid=" + document.selector.branchid.value;
} else {
searchlink = searchlink + "&branchid=" + 0;
}

i get the error "Object expected" on the line with the if statement.

can anyone tell me where i'm going wrong.

thanks

Emma
 
There is no built-in isNumeric() function like you're trying to use. Javascript is object-oriented, and methods (functions) are related to objects. To do what you want to do, you'll have to write your own isNumeric function to check for whatever characters and sequences you want it to check for.

You could write something like:

function isNumeric(onevalue)
{
var digit='0123456789.,';

for (var oi=0;oi<onevalue.length;oi++)
{
var onechar=onevalue.substr(oi, 1);
if (digit.indexOf(onechar)==-1) return false;
}

return true;
}

This is compatible with older versions of Javascript, too.
 
thats fantastic!

think i must have been getting confused with my asp.


thanks so much for your help :-D

Emma
 
There's no such a built-in javascript function isNumeric().
You can use these:
parseFloat(), parseInt(), isNaN() functions
or NaN property.

parseInt() parses a string argument and returns an integer number.
parseFloat() parses a string argument and returns a floating point number.
isNaN() evaluates an argument to determine if it is &quot;NaN&quot; (Not-a-Number).

Change your code to this:

if (!isNaN(document.selector.branchid.value))

Don't forget about case-sensitivity in javascript, all function names should be typed exactly as I did here.
good luck
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top