www.webdeveloper.com
Results 1 to 10 of 10

Thread: Trouble with alert box instead of return true/false

  1. #1
    Join Date
    Aug 2012
    Posts
    5

    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;
    }

  2. #2
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,377
    What do you mean by a custom alert? What is it supposed to look like, what do you expect it to do?

    You could tie a hidden <div> tag and it's contents to an element of the page.

  3. #3
    Join Date
    Aug 2012
    Posts
    5
    Right now it pops up a box, true or false. I'd like to change the words in those boxes to something else.

  4. #4
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,377
    Provide some brief code showing what it does now and how/where you want it to be different.

  5. #5
    Join Date
    Aug 2012
    Posts
    5
    I did provide the function in my op. Do you want the rest that performs the calculation?

  6. #6
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,377

    Question

    Quote Originally Posted by swansong View Post
    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?

  7. #7
    Join Date
    Aug 2012
    Posts
    5
    I'm sorry, the entire code is below.

    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 &#37; 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;
    }

  8. #8
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,377

    Lightbulb

    How about ...
    Code:
    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.
    Last edited by JMRKER; 08-22-2012 at 09:20 AM.

  9. #9
    Join Date
    Aug 2012
    Posts
    5
    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.

  10. #10
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,377

    Exclamation

    Quote Originally Posted by swansong View Post
    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?

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles