Question

Topic: Other

Required Field In Forms

Posted by Anonymous on 125 Points
I'm making forms for student registration and event reservations and want to know the best way to make certain fields required, so that the user cannot submit the form until they're filled in.
To continue reading this question and the solution, sign up ... it's free!

RESPONSES

  • Posted by Blaine Wilkerson on Member
    Here is a link to a java script used for this very thing. Just alter the title of the questions in the commands to match the fields you require complete on your forms. (i.e. "Name", "Email", etc. You can duplicate the commands to make more required fields)

    https://javascript.internet.com/forms/required-fields.html

    I hope this helps.

    Good Luck!
  • Posted by Peter (henna gaijin) on Member
    I am assuming that you are doing this in a web site? If so, you need to use a programming language beyond HTML to do this.

    For example, I use the following Javascript in the head section of HTML to check that an first and last name are not blank:

    <SCRIPT TYPE="text/javascript">

    function validateForm()
    {
    var error_string = "";
    if(window.document.demoForm.first.value == "")
    { error_string += " - Please enter your First Name. \n"; }
    if(window.document.demoForm.last.value == "")
    { error_string += " - Please enter your Last Name. \n"; }

    //check error_string
    if(error_string == "")
    { return true; } else {
    error_string2 = "Complete the follow in order to submit: \n" + error_string;
    alert(error_string2);
    return false; }
    }
    //-->
    </SCRIPT>



    It would be called in the <FORM> statement, such as:

    <form NAME="demoForm" enctype="multipart/form-data" method="post" action="filename" onSubmit="var results = validateForm(); return results;">

    And the following <input/> statements inside the form:
    <INPUT TYPE="TEXT" NAME="first" SIZE="30" MAXLENGTH="255">
    <INPUT TYPE="TEXT" NAME="last" SIZE="30" MAXLENGTH="255">


    This whole set together would cause a popup window to appear if the field is left blank, telling them which field they need to fill in.

    To work with variables, you are likely working in PHP or CGI. You could always have the page which is called on when you submit the form do the check. I usually do it on both pages, becasue supposedly some people have Javascript turned off, so there is no guarantee the one above will catch all errors.
  • Posted by Blaine Wilkerson on Member


    IPT LANGUAGE="JavaScript">






    ipt>







    name=form method="post" action="">
    First Name

    Last Name

    E-Mail






    >


    Free JavaScripts provided

    by The JavaScript Source


Post a Comment