Welcome to WebmasterWorld Guest from 3.234.210.89

Forum Moderators: open

Message Too Old, No Replies

javascript data validation - for currency input

debugging the code

     
5:30 pm on Apr 18, 2003 (gmt 0)

New User

10+ Year Member

joined:Feb 4, 2003
posts:18
votes: 0


hi
Can anybody tell me if this code for javascript correct.
this is to check if the values entered in the form field are only digits or . or ,

if(sell_books.price.value=="")
{
alert("Please enter the price of the Book");
sell_books.price.focus();
return false;
}
var char= "0123456789.,";
var priceValue = sell_books.price.value ;
if (i=0; i<priceValue.length; i++)
{
if (j=0; j<char.length; j++)
{
priceValue.charAt(i)!= char.charAt(j);
alert ("Please enter the valid digit")
return false;
}
}

form name="sell_books"

9:57 pm on Apr 18, 2003 (gmt 0)

Junior Member

10+ Year Member

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


You shouldn't rely on those JavaScript checks and check any user submitted values again in your server side script. Otherwise, you really should treat any user submitted data as possibly insecure.

Anyways, your script looks on the face of it ok, though it would not flag "00--00,,..8" as an error. Maybe you could try-

function isNumber(str){
if(str.length==0)
{return false;}
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;
}// end isNumber function

function formTriggeredEvent{
if(!isNumber(sell_books.price.value))
{
alert ("Please enter the valid number.")
}
else
{
//form data is OK
form.submit();
}
}

4:42 am on Apr 19, 2003 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Mar 27, 2003
posts:664
votes: 0


dskaushik5, I can spot a few syntax errors:

- missing semicolon after alert() and on the last line
- two of the 'if' statements should probably be 'for' statements

In addition, there are a whole lot of logical errors! For example, 'priceValue.charAt(i)!= char.charAt(j);' won't do anything, so the code will probably just give you the alert 12x(length of input string) times, no matter what the input string was. What you probably meant was to put the 'priceValue.charAt(i)!= char.charAt(j);' in an 'if' statement, but that won't work. For example, if the string you are testing is 23.17, your code will test whether '0' is not equal to '2', and then fire the alert, even though 2 is a perfectly legal digit. Try do a walk-through with pen and paper of your code to debug it (i.e. pretend you are a computer...).

If you are trying to learn Javascript, in addition to doing a walk-through of HocusPocus' suggested code, it might be an idea to look at one of the threads here: [webmasterworld.com...]

There are many ways to skin a cat. The way I'd go would be to define a regular expression, and test:


function isNumber(str) {
isPrice = /^\d+\.\d{2}$/;
return isPrice.test( str );
}

function formTriggeredEvent{
if(!isNumber(sell_books.price.value))
{
alert ("You bozo...")
}
else
{
//form data is OK
form.submit();
}

Shawn