Results 1 to 3 of 3

Thread: How to call a function within a function??

  1. #1
    Join Date
    Sep 2006
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation How to call a function within a function??

    I have two functions, check_info() and CheckCardNumber()
    The first one is to validate forms with names and address.
    The second one is to validate credit card numbers.
    I call the first function to check all the forms except credit cards numbers.
    I'm wondering if there's a way to call the credit card function at the same time.
    I tried
    <input type="submit" value="Submit" onclick="return check_info(); CheckCardNumber(this.form)" disabled>
    but it works separately. It executes the first function and then executes the second not simultaneously.

    I don't know if there's a way I can call the CheckCardNumber() function within the check_info() function so that it can execute at the same time.
    Please help

    Thanks

    Following are the functions' code


    <The first function------------------------------------------------------->

    <SCRIPT LANGUAGE="JavaScript">
    function check_email()

    {

    if (document.myform2.email1.value != "")

    {

    email_addr=new String(document.myform2.email1.value)

    var the_length=email_addr.length;
    var last_char=email_addr.charAt(the_length-1);




    if (email_addr.indexOf('@',0)==-1 || email_addr.length < 5 || email_addr.indexOf('.',0)==-1 || last_char== '.')

    {
    return 1;
    }


    if (email_addr.indexOf('aiuonline',0) > -1)

    {
    return 3;
    }


    if (email_addr.indexOf('@',0) != -1)

    {
    check_point = email_addr.indexOf('@',0)

    a = new Array()

    m = 0

    b= "abcdefghijklmnopqrstuvwxyz"

    for (i=0;i<26;i++)

    {
    a[i]=b.charAt(i)
    }

    for (i=0;i<26;i++)

    {
    email_addr=email_addr.toLowerCase()



    if (email_addr.indexOf(a[i],check_point) != -1) break
    m=m+1

    }


    if (m==26)
    {

    return 2;
    }


    }

    }

    }

    function check_info()


    {


    if (document.myform2.sFirstName.value == "")
    {
    alert("Please enter your firstname");
    document.myform2.sFirstName.focus();
    return false;

    }
    if (document.myform2.sLastName.value == "")
    {
    alert("Please enter your lastname");
    document.myform2.sLastName.focus();
    return false;

    }


    if (document.myform2.sZip1.value.length < 5)
    {

    alert("Please provide valid zip code");
    document.myform2.sZip1.focus();
    return false;

    }


    if (document.myform2.id.value == "")
    {
    alert("Please enter your desired ID");
    document.myform2.id.focus();
    return false;
    }

    if (document.myform2.id.value.length < 4)
    {

    alert("ID must be at least 4 characters");
    document.myform2.id.focus();
    return false;

    }

    if (document.myform2.pd1.value == "")
    {

    alert("Please enter your password");
    document.myform2.pd1.focus();
    return false;

    }




    <The second function------------------------------------------------>

    <SCRIPT LANGUAGE="JavaScript">

    <!-- Begin
    var Cards = new makeArray(8);
    Cards[0] = new CardType("MasterCard", "51,52,53,54,55", "16");
    var MasterCard = Cards[0];
    Cards[1] = new CardType("VisaCard", "4", "13,16");
    var VisaCard = Cards[1];
    Cards[2] = new CardType("AmExCard", "34,37", "15");
    var AmExCard = Cards[2];
    Cards[3] = new CardType("DinersClubCard", "30,36,38", "14");
    var DinersClubCard = Cards[3];
    Cards[4] = new CardType("DiscoverCard", "6011", "16");
    var DiscoverCard = Cards[4];
    Cards[5] = new CardType("enRouteCard", "2014,2149", "15");
    var enRouteCard = Cards[5];
    Cards[6] = new CardType("JCBCard", "3088,3096,3112,3158,3337,3528", "16");
    var JCBCard = Cards[6];
    var LuhnCheckSum = Cards[7] = new CardType();
    CheckCardNumber(form)
    function called when users click the "check" button.

    function CheckCardNumber(form) {
    var tmpyear;
    if (form.CardNumber.value.length == 0) {
    alert("Please enter a Card Number.");
    form.CardNumber.focus();
    return;
    }
    if (form.ExpYear.value.length == 0) {
    alert("Please enter the Expiration Year.");
    form.ExpYear.focus();
    return;
    }
    if (form.ExpYear.value > 96)
    tmpyear = "19" + form.ExpYear.value;
    else if (form.ExpYear.value < 21)
    tmpyear = "20" + form.ExpYear.value;
    else {
    alert("The Expiration Year is not valid.");
    return;
    }
    tmpmonth = form.ExpMon.options[form.ExpMon.selectedIndex].value;
    // The following line doesn't work in IE3, you need to change it
    // to something like "(new CardType())...".
    // if (!CardType().isExpiryDate(tmpyear, tmpmonth)) {
    if (!(new CardType()).isExpiryDate(tmpyear, tmpmonth)) {
    alert("This card has already expired.");
    return;
    }
    card = form.CardType.options[form.CardType.selectedIndex].value;
    var retval = eval(card + ".checkCardNumber(\"" + form.CardNumber.value +
    "\", " + tmpyear + ", " + tmpmonth + ");");
    cardname = "";
    if (retval)

  2. #2
    Join Date
    Sep 2005
    Posts
    882
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default

    What does it matter if it happens seperatly? The script should run fast enough that you never notice.

  3. #3
    Join Date
    May 2006
    Location
    Sydney, Australia - Near the coast.
    Posts
    1,995
    Thanks
    0
    Thanked 8 Times in 7 Posts

    Default

    Please: Please wrap your code in the [code] or [html] tags.
    Peter - alotofstuffhere[dot]com - Email Me - Donate via PayPal - Got spare hardware? Donate 'em to me :) Just send me a PM.
    Currently: enjoying the early holidays :)
    Read before posting: FAQ | What you CAN'T do with JavaScript | Form Rules | Thread Title Naming Guide

Bookmarks

Posting Permissions

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