Welcome to WebmasterWorld Guest from 54.162.226.212

Forum Moderators: open

Message Too Old, No Replies

Javascript Error

Numeric Validation Fails

     
8:55 pm on Mar 5, 2003 (gmt 0)

10+ Year Member



The following code is failing for some reason:

var error = 0;
var error_message = "Errors have occured during the process of your form!\nPlease make the following corrections:\n\n";
var bid = document.make_bid.customer_bid.value;

if (IsNumeric(bid) == false) {
error_message = error_message + "* Bid must be numeric!\n";
error = 1;
}

bid gets the value of document.make_bid.customer_bid.value ok but the statement "if (IsNumeric(bid) == false)" is failing whether the input field contents are numeric or not!

Any help gratefully received!

9:02 pm on Mar 5, 2003 (gmt 0)

10+ Year Member



Sorry folks, it doesn't work because I am stupid!
I forgot to include the following code (forgetting that IsNumeric isn't a javascript function):

function IsNumeric(strString)
// check for valid numeric strings
{
var strValidChars = "0123456789.-";
var strChar;
var blnResult = true;

if (strString.length == 0) return false;

// test strString consists of valid characters listed above
for (i = 0; i < strString.length && blnResult == true; i++)
{
strChar = strString.charAt(i);
if (strValidChars.indexOf(strChar) == -1)
{
blnResult = false;
}
}
return blnResult;
}

12:42 pm on Mar 6, 2003 (gmt 0)

10+ Year Member



IsNumeric("0-0-2...5-1") returns true;

Guessing that a negative Bid is not accepatble i would use this function instead

function isNumber(str){
numdecs = 0;
for (i = 0; i < str.length; i++){
mychar = str.charAt(i);
if ((mychar >= "0" && mychar <= "9") mychar == "."){
if (mychar == ".")
numdecs++;
}
else return false;
}

if (numdecs > 1)
return false;

return true;
}

12:47 pm on Mar 6, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I hope you do not rely on those JavaScript checks and check any user submitted values again in your server side script. If not, you really should treat any user submitted data as tainted and highly insecure.

Andreas

 

Featured Threads

Hot Threads This Week

Hot Threads This Month