Forum Moderators: open
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"
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();
}
}
- 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