SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Zealot
    Join Date
    Mar 2011
    Posts
    146
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Lightbulb on submit multiple functions

    Hi,

    Does anyone know of a way to get onSubmit to run multiple functions?

    also is there a way to make this nBlank function smaller?

    Alternative suggestions?

    function nBlank()
    {
    if (document.Forders.fName.value.length < 2 )
    {
    alert("Please enter your name");
    return false;
    }
    if (document.Forders.fName.value == "Please enter your name" )
    {
    alert("Please enter your name");
    return false;
    }
    if (document.Forders.address.value.length < 2 )
    {
    alert("Please enter your address");
    return false;
    }
    if (document.Forders.address.value == "Please enter your address" )
    {
    alert("Please enter your address");
    return false;
    }
    if (document.Forders.suburb.value.length < 2 )
    {
    alert("Please enter your suburb");
    return false;
    }
    if (document.Forders.suburb.value == "Suburb" )
    {
    alert("Please enter your suburb");
    return false;
    }
    }

  2. #2
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,191
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Yep its very simple, basically all you need to do is add a colon ; to the end of the function, see the example below.

    HTML Code:
    <form onsubmit="func1();func2();func3();return false;">
    Below is a more simplified version of your nBlank function but it will run exactly the same way.

    Code JavaScript:
    function nBlank()
    {
        var name = document.Forders.fName.value,
            address = document.Forders.address.value,
            suburb = document.Forders.suburb.value;
     
        if (name.length < 2 || name == "Please enter your name")
        {
            alert("Please enter your name");
            return false;
        }
     
        if (address.length < 2 || address == "Please enter your address")
        {
            alert("Please enter your address");
            return false;
        }
     
        if (suburb.length < 2 || suburb == "Suburb")
        {
            alert("Please enter your suburb");
            return false;
        }
    }

  3. #3
    SitePoint Zealot
    Join Date
    Mar 2011
    Posts
    146
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks very much Sgt,

    just what i needed

  4. #4
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,701
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    If the field values are already assigned when the page starts, you can check against the defaultValue property of the field. We can also loop through an array of field names to check. That allows us to condense your function down to just this:

    Code JavaScript:
    function nBlank() {
        var fieldsToCheck = ['fName', 'address', 'suburb'],
            field,
            i;
        for (i = 0; i < fieldsToCheck.length; i += 1) {
            field = document.Forders[fieldsToCheck[i]];
            if (field.value.length < 2 || field.value === field.defaultValue) {
                alert(field.defaultValue);
            }
        }
    }
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


Tags for this Thread

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
  •