homepage Welcome to WebmasterWorld Guest from 54.211.73.232
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

    
javascript data validation - for currency input
debugging the code
dskaushik5

10+ Year Member



 
Msg#: 375 posted 5:30 pm on Apr 18, 2003 (gmt 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"

 

HocusPocus

10+ Year Member



 
Msg#: 375 posted 9:57 pm on Apr 18, 2003 (gmt 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();
}
}

ShawnR

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 375 posted 4:42 am on Apr 19, 2003 (gmt 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

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved