Trouble with alert box instead of return true/false
I have a function something like the below. Instead of a box popping up saying "true" or "false" I'd like to display a custom alert.
I tried adding an alert('blabla'); before the return true... and that pops-up, but then I also get a "true" pop-up. I also tried adding a return false after my alert box but that didn't help either.
Can someone give me a hand?
function Validate(test)
{
var LuhnDigit = parseInt(Luhn.substring(Luhn.length-1,Luhn.length));
var LuhnLess = Luhn.substring(0,Luhn.length-1);
if (Calculate(LuhnLess)==parseInt(LuhnDigit))
{
return true;
}
return false;
}
I did provide the function in my op. Do you want the rest that performs the calculation?
So many questions, so little time ....
Where is 'Luhn' defined and what does it contain?
What calculations are being done in the Calculate() function?
What is the custom alert supposed to look like? Contents?
In your original post, you used Validate(test) as an argument to the function. Where did 'test' come from and why did you not use it in the function if you wanted to pass it?
When the validate function is called it returns true or false... I want to change the "true" pop-up to something else... (some other words) and the same with false.
function Calculate(Luhn)
{
var sum = 0;
for (i=0; i<Luhn.length; i++ )
{
sum += parseInt(Luhn.substring(i,i+1));
}
var delta = new Array (0,1,2,3,4,-4,-3,-2,-1,0);
for (i=Luhn.length-1; i>=0; i-=2 )
{
var deltaIndex = parseInt(Luhn.substring(i,i+1));
var deltaValue = delta[deltaIndex];
sum += deltaValue;
}
var mod10 = sum % 10;
mod10 = 10 - mod10;
if (mod10==10)
{
mod10=0;
}
return mod10;
}
function Validate(Luhn)
{
var LuhnDigit = parseInt(Luhn.substring(Luhn.length-1,Luhn.length));
var LuhnLess = Luhn.substring(0,Luhn.length-1);
if (Calculate(LuhnLess)==parseInt(LuhnDigit))
{
return true;
}
return false;
}
function Validate(Luhn) {
var LuhnDigit = parseInt(Luhn.substring(Luhn.length-1,Luhn.length));
var LuhnLess = Luhn.substring(0,Luhn.length-1);
if (Calculate(LuhnLess)==parseInt(LuhnDigit)) { alert('Success'); } else { alert('Not so successful'); }
}
Note also that your Calculate() function returns true or false, but it makes the comparison to be equal to an integer values from parseInt() function.
Since the boolean will never equal to the digit returned, you are destined to always get a false for that 'if' statement.
BTW: You should enclose your code between [ code] and [ /code] tags (without the spaces)
to make it easier to read, copy, test and retain format for other forum members.
I tried something similar, and tried your code but I still get a second "undefined" pop-up after the success or not success. Is there no way to surpress that?
I tried something similar, and tried your code but I still get a second "undefined" pop-up after the success or not success. Is there no way to surpress that?
Thanks.
My problem in trying to answer your questions revolves around trying to give full answers after being given snippets of code.
There are other areas of your code that may be giving you the 'undefined' display. Instead of saying something "didn't work", show us what you used that didn't work.
I can guess all day and solve the problem to my satisfaction, but it may not meet your criteria of success. Do you have a link to some live code?
Did you look at the error console to see which line(s) might give you an error?
Bookmarks