Menu

Required Fields

Help
Keelhauler
2008-03-26
2013-06-03
  • Keelhauler

    Keelhauler - 2008-03-26

    I created a form and modified it.
    I would like to add a required field. Where, and in which file(s) is this code located.
    form.html
    processor.php
    install.php

    John

     
    • TNTEverett

      TNTEverett - 2008-03-27

      At the bottom of the form.html file you will find lines like this:

      if (validateField('field_1','fieldBox_1','text',0)

      One for each form entry.  Changing the last ,0 to ,1 will change the field from not required to required.

       
    • Keelhauler

      Keelhauler - 2008-03-27

      What if it is a checkbox?
      if (validateField('field_2','fieldBox_2','checkbox',1) == false)

      This does not seem to work, it does not require a check in this fieldbox 2

       
      • TNTEverett

        TNTEverett - 2008-03-28

        Validating a checkbox is more complex.  You probably do not want to require every box be checked.  You may or may not want 1 or more check boxes as required. 
        Most typically you would want to have a requirement that only one of many check boxes required.  So depending on the number of boxes and the number of desired required boxes your form code would have to be custom.  Since this level of customization would require many many many variations of code generation, it is left to the user to define and implement whatever customization they deem necessary. 
        You are correct, check boxes have no current validation checking feature to ensure that a check box selection is required.  Look in the form.html file for the Java code that does form field requirements checking.  This is what you will find.

                    function validateField(fieldId, fieldBoxId, fieldType, required)
                    {
                        fieldBox = document.getElementById(fieldBoxId);
                        fieldObj = document.getElementById(fieldId);
                        if(fieldType == 'text'  ||  fieldType == 'textarea'  ||  fieldType == 'password'  ||  fieldType == 'file'  ||  fieldType == 'phone'  || fieldType == 'website')
                        {  
                            if(required == 1 && fieldObj.value == '')
                            {
                                fieldObj.setAttribute("class","mainFormError");
                                fieldObj.setAttribute("className","mainFormError");
                                fieldObj.focus();
                                return false;                  
                            }
                        }
                        else if(fieldType == 'menu'  || fieldType == 'country'  || fieldType == 'state')
                        {  
                            if(required == 1 && fieldObj.selectedIndex == 0)
                            {              
                                fieldObj.setAttribute("class","mainFormError");
                                fieldObj.setAttribute("className","mainFormError");
                                fieldObj.focus();
                                return false;                  
                            }
                        }
                        else if(fieldType == 'email')
                        {  
                            if((required == 1 && fieldObj.value=='')  ||  (fieldObj.value!=''  && !validate_email(fieldObj.value)))
                            {              
                                fieldObj.setAttribute("class","mainFormError");
                                fieldObj.setAttribute("className","mainFormError");
                                fieldObj.focus();
                                return false;                  
                            }
                        }
                    }

        As you can see
        fieldType == 'checkbox'
        can not be found in this validation code. 

         
    • Keelhauler

      Keelhauler - 2008-03-28

      There is only one checkbox that I need to require being checked.
      "They agree to terms"

      So do I just add  || fieldType == 'checkbox' in the sixth line of your example? Or some where else as well?

      Would a radio button be better? Yes or no answer, I don't see that "type=radio name" is an option either.

       
      • TNTEverett

        TNTEverett - 2008-03-28

        I did not provide an example.  I copied actual code from the form.html file.  This code iscommon to every form and validates fields selected as "required".  To make one of your check boxes required will require custom code. 
        If you are asking me if I can provide custom code for you the answer is yes I can but you will have to email me a link to your form and a copy of your processor.php file. 
        What you are looking for seems simple enough. 

         

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.