If Validation = True, Submit

A

Anonymous

Guest
Hello,

I'm building a web app the processes information into a DB. I'm attempting to write a JS validation page. On this page I'm also trying to make the button perform a submit only if the validation returns as true. How do I make this happen? So far, I have this for Javascript:
Code:
function validateForm(){
    var category = document.forms["maintable"]["Category"].value;
    var proName = document.forms["maintable"]["ProName"].value;
    //var misc = document.forms["maintable"]["Misc"].value; //This might become a requirement in the future
    var dateInput = document.forms["maintable"]["dateInput"].value;
    var time = document.forms["maintable"]["time"].value;
    var sampleID = document.forms["maintable"]["sampleID"].value;
    var panNumber= document.forms["maintable"]["panNumber"].value;
    var panWT = document.forms["maintable"]["PanWT"].value;
    var panSampWT = document.forms["maintable"]["PanSampWT"].value;
    var moistPercent = document.forms["maintable"]["MoisturePercent"].value;
    var dryWT = document.forms["maintable"]["DryWT"].value;
    var minSpec = document.forms["maintable"]["MinSpec"].value;
    var maxSpec = document.forms["maintable"]["MaxSpec"].value;
    
    if(category == null || category == ""){
        alert("The \"Find CAT\" field is blank. \"Find CAT\" must have a value!")
        return false;
    }
    if(proName == null || proName == ""){
        alert("The NAME field is blank. The NAME field must have a value!")
        return false;
    }
   // if(misc == null || misc == ""){
//        alert("The MISC field is blank. MISC must have a value!")
//        return false;
//    }
    if(dateInput == null || dateInput == ""){
        alert("The Date field is blank. \"Date\" must have a value!")
        return false;
    }
    if(time == null || time == ""){
        alert("The TIME field is blank. \"TIME\" must have a value!")
        return false;
    }
    if(sampleID == null || sampleID == ""){
        alert("The \"SAMPLE ID\" field is blank. \"SAMPLE ID\" must have a value!")
        return false;
    }
    if(panNumber == null || panNumber == ""){
        alert("The \"PANNO\" field is blank. There must be a Pan Number!")
        return false;
    }
    if(panWT == null || panWT == ""){
        alert("The \"PAN WEIGHT\" field is blank. There must be a Pan Weight!")
        return false;
    }
    if(panSampWT == null || panSampWT == ""){
        alert("The \"PAN + SAMPLE WEIGHT\" field is blank. There must be a value!")
        return false;
    }
    if(moistPercent == null || moistPercent == ""){
        alert("The \"% MOISTURE\" field is blank. Do you have all of the fields that require this calculation populated with a value? Please populate all of the fields required!")
        return false;
    }
    if(dryWT == null || dryWT == ""){
        alert("The \"DRIED WEIGHT\" field is blank. There must be a value in \"DREID WEIGHT!\" ")
        return false;
    }
    if(minSpec == null || minSpec == ""){
        alert("The \"MINSPEC\" field is blank. How are you supposed to tell if the product is in range if there isn\'t a minimum spec? Please select a product to get the minimum spec!")
        return false;
    }
    if(maxSpec == null || maxSpec == ""){
        alert("The \"MAXSPEC\" field is blank. How are you supposed to tell if the product is in range if there isn\'t a maximum spec? Please seslect a product to get the maximum spec!")
        return false;
    }
}

Somehow I would like to say if all fields are true then submit to submission.php. Could anyone give me a hand with this?

Thanks,
dlopez
 
simply add a return true before your last curly bracket.
 
Here's the issue with that. I have a regular button and not a submit button.

So:
Code:
<td><input type="button" value="Submit" onclick="submitForm()" /></td>

If I were to use a submit button, the action method in the form (submission.php) would navigate on submit anyway. I'm attempting to make the javascript say that if all fields are filled out, then the navigate to the php page. If not, stay where you are and don't change anything on the page (so that everything that has already been filled out, stays). So basically, I want the action of this button to be controlled by a javascript function. Therefore, the javascript telling it whether it should redirect, or not.
 
I figured it out.. Thanks for the help. I was attempting to over-complicate the issue.
 
Back
Top