Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    May 2004
    Posts
    73
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Help with checking required fields

    Hi, I'm a n00b.

    So I want to check all the fields on the page to see if they've all got selections. But because PHP outputs only some of the total number of fields, dynamically based on user input on previous pages, I don't know which fields will be there and which aren't.

    I really only barely know javascript, but it seems to me like this should work:

    function validate()
    {
    if (mainform.associate_weight.value) { associate_weight = mainform.associate_weight.value; }
    else { associate_weight = 1; }
    if (mainform.customer_weight.value) { customer_weight = mainform.customer_weight.value; }
    else { customer_weight = 1; }
    if (mainform.problem_weight.value) { problem_weight = mainform.problem_weight.value; }
    else { problem_weight = 1; }
    if (mainform.business_weight.value) { business_weight = mainform.business_weight.value; }
    else { business_weight = 1; }
    if (mainform.technical_weight.value) { technical_weight = mainform.technical_weight.value; }
    else { technical_weight = 1; }
    if (mainform.adaptability_weight.value) { adaptability_weight = mainform.adaptability_weight.value; }
    else { adaptability_weight = 1; }
    if (mainform.cognitive_weight.value) { cognitive_weight = mainform.cognitive_weight.value; }
    else { cognitive_weight = 1; }
    if (mainform.organizational_weight.value) { organizational_weight = mainform.organizational_weight.value; }
    else { organizational_weight = 1; }
    if (mainform.self_weight.value) { self_weight = mainform.self_weight.value; }
    else { self_weight = 1; }
    if (mainform.teamwork_weight.value) { teamwork_weight = mainform.teamwork_weight.value; }
    else { teamwork_weight = 1; }
    if (mainform.staff_weight.value) { staff_weight = mainform.staff_weight.value; }
    else { staff_weight = 1; }
    if (mainform.financial_weight.value) { financial_weight = mainform.financial_weight.value; }
    else { financial_weight = 1; }
    if (mainform.client_weight.value) { client_weight = mainform.client_weight.value; }
    else { client_weight = 1; }
    if (mainform.operations_weight.value) { operations_weight = mainform.operations_weight.value; }
    else { operations_weight = 1; }
    if (mainform.personal_weight.value) { personal_weight = mainform.personal_weight.value; }
    else { personal_weight = 1; }
    if (mainform.operations_weight.value) { operations_weight = mainform.operations_weight.value; }
    else { operations_weight = 1; }
    if (mainform.personal_weight.value) { personal_weight = mainform.personal_weight.value; }
    else { personal_weight = 1; }
    if (mainform.department_weight.value) { department_weight = mainform.department_weight.value; }
    else { department_weight = 1; }

    if (mainform.associate_grade.value) { associate_grade = mainform.associate_grade.value; }
    else { associate_grade = 1; }
    if (mainform.customer_grade.value) { customer_grade = mainform.customer_grade.value; }
    else { customer_grade = 1; }
    if (mainform.problem_grade.value) { problem_grade = mainform.problem_grade.value; }
    else { problem_grade = 1; }
    if (mainform.business_grade.value) { business_grade = mainform.business_grade.value; }
    else { business_grade = 1; }
    if (mainform.technical_grade.value) { technical_grade = mainform.technical_grade.value; }
    else { technical_grade = 1; }
    if (mainform.adaptability_grade.value) { adaptability_grade = mainform.adaptability_grade.value; }
    else { adaptability_grade = 1; }
    if (mainform.cognitive_grade.value) { cognitive_grade = mainform.cognitive_grade.value; }
    else { cognitive_grade = 1; }
    if (mainform.organizational_grade.value) { organizational_grade = mainform.organizational_grade.value; }
    else { organizational_grade = 1; }
    if (mainform.self_grade.value) { self_grade = mainform.self_grade.value; }
    else { self_grade = 1; }
    if (mainform.teamwork_grade.value) { teamwork_grade = mainform.teamwork_grade.value; }
    else { teamwork_grade = 1; }
    if (mainform.staff_grade.value) { staff_grade = mainform.staff_grade.value; }
    else { staff_grade = 1; }
    if (mainform.financial_grade.value) { financial_grade = mainform.financial_grade.value; }
    else { financial_grade = 1; }
    if (mainform.client_grade.value) { client_grade = mainform.client_grade.value; }
    else { client_grade = 1; }
    if (mainform.operations_grade.value) { operations_grade = mainform.operations_grade.value; }
    else { operations_grade = 1; }
    if (mainform.personal_grade.value) { personal_grade = mainform.personal_grade.value; }
    else { personal_grade = 1; }
    if (mainform.operations_grade.value) { operations_grade = mainform.operations_grade.value; }
    else { operations_grade = 1; }
    if (mainform.personal_grade.value) { personal_grade = mainform.personal_grade.value; }
    else { personal_grade = 1; }
    if (mainform.department_grade.value) { department_grade = mainform.department_grade.value; }
    else { department_grade = 1; }

    var validateArray = new Array(associate_weight, customer_weight, problem_weight, business_weight, technical_weight, adaptability_weight, cognitive_weight, organizational_weight, self_weight, teamwork_weight, staff_weight, financial_weight, client_weight, operations_weight, personal_weight, department_weight, associate_grade, customer_grade, problem_grade, business_grade, technical_grade, adaptability_grade, cognitive_grade, organizational_grade, self_grade, teamwork_grade, staff_grade, financial_grade, client_grade, operations_grade, personal_grade, department_grade);

    fail=0;

    for (var i=0; i<validateArray.length; i++)
    {
    arrayItem=validateArray[i];
    if (arrayItem=='')
    {
    fail=1;
    }
    }

    if (fail==1)
    {
    alert('Please complete all fields.');
    event.returnValue=false;
    }
    }

    But it doesn't. Can anyone help out a n00b?

  • #2
    New Coder
    Join Date
    Jun 2004
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Lightbulb Simple Field Existance check

    Don't worry "N00bs" are people too.

    questor,

    You can use a quick DOM call to check to see if the object exists in the document.


    Code:
    for (var i=0; i<validateArray.length; i++){
    	oField = document.forms[0].elements[validateArray[i]]
    	if ((oField)){
    		// Add Validation Code Here
    	}
    }
    If the field was output by PHP's echo statement then the statement will return [object HTMLInputElement] and evaluate as true.

    An alternative is to place your validation script at the end of the page. That way PHP can output a custom array of only the generated form fields. Althought doing so would require adding server side code, the change would have the advantage of speeding up the validation process as you would not have to a smaller array evaluate and loop thru.




    MindCrafter
    www.mindcrafter.com

  • #3
    New Coder
    Join Date
    May 2004
    Posts
    73
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hmm, I've got:

    function validate()
    {
    var validateArray = new Array(associate_weight, customer_weight, problem_weight, business_weight, technical_weight, adaptability_weight, cognitive_weight, organizational_weight, self_weight, teamwork_weight, staff_weight, financial_weight, client_weight, operations_weight, personal_weight, department_weight, associate_grade, customer_grade, problem_grade, business_grade, technical_grade, adaptability_grade, cognitive_grade, organizational_grade, self_grade, teamwork_grade, staff_grade, financial_grade, client_grade, operations_grade, personal_grade, department_grade);

    fail=0;

    for (var i=0; i<validateArray.length; i++)
    {
    oField = document.forms[0].elements[validateArray[i]]
    if ((oField))
    {
    if (oField=='')
    {
    fail=1;
    }
    }
    }

    if (fail==1)
    {
    alert('Please complete all fields.');
    event.returnValue=false;
    }
    }

    And it seems to let me to the next page without question...?

  • #4
    Regular Coder
    Join Date
    Jun 2002
    Location
    The Planet Earth Code Poet: True
    Posts
    282
    Thanks
    0
    Thanked 1 Time in 1 Post
    Your array contains a bunch of strings so you need to enclose them in quotes. You also never check the value. Try something like this:

    Code:
    function validate(){
    	var validateArray = new Array("associate_weight","customer_weight","problem_weight","business_weight","technical_weight","adaptability_weight","cognitive_weight","organizational_weight","self_weight","teamwork_weight","staff_weight","financial_weight","client_weight","operations_weight","personal_weight","department_weight","associate_grade","customer_grade","problem_grade","business_grade","technical_grade","adaptability_grade","cognitive_grade","organizational_grade","self_grade","teamwork_grade","staff_grade","financial_grade","client_grade","operations_grade","personal_grade","department_grade");
    
    	fail=0;
    
    	for (var i=0; i<validateArray.length; i++){
    		oField = document.forms[0].elements[validateArray[i]]
    		if ((oField)){
    			if (oField.value=='') fail=1;
    		}
    	}
    
    	if (fail){
    		alert('Please complete all fields.');
    		event.returnValue=false;
    	}
    }

  • #5
    New Coder
    Join Date
    May 2004
    Posts
    73
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You da man!

  • #6
    Regular Coder
    Join Date
    Aug 2002
    Location
    USA
    Posts
    625
    Thanks
    0
    Thanked 0 Times in 0 Posts
    <script>
    <!--
    function checkForm(){
    for(i=0;i<mainform.elements.length;i++){
    switch (mainform.elements[i].value){
    case "": alert("You must complete all fields!");
    return false;
    }
    }
    }
    //-->
    </script>




    -james
    Last edited by jamescover; 07-04-2004 at 07:06 PM.
    "God so loved the world that he gave his only begotten son, so that whosoever believed in him would not perish, but have everlasting life. For God did not send his son into the world to condemn the world, but so that through him the world might be saved. "


  •  

    Posting Permissions

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