Welcome to WebmasterWorld Guest from 54.158.55.251

Forum Moderators: open

Message Too Old, No Replies

Javascript Error

Numeric Validation Fails

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

Preferred Member

10+ Year Member

joined:Sept 29, 2002
posts:356
votes: 0


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)

Preferred Member

10+ Year Member

joined:Sept 29, 2002
posts:356
votes: 0


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)

Junior Member

10+ Year Member

joined:Dec 27, 2002
posts:125
votes: 0


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)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:July 22, 2002
posts:1782
votes: 0


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

 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members