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?:smiley:

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

Yep its very simple, basically all you need to do is add a colon ; to the end of the function, see the example below.

<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.

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

Thanks very much Sgt,

just what i needed :smiley:

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:


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