85 users online (0 members and 85 guests)  




Page 1 of 2 12 Last
  Results 1 to 15 of 18
Digg button stumble

Related

  1. HTML Form with required fields    Forum: HTML Forum
    Replies: 10
  2. Required Form Fields    Forum: Javascript Forum
    Replies: 1
  3. Replies: 1
  4. Replies: 3
  1. #1
    san's Avatar
    New User

    Status
    Offline
    Join Date
    Mar 2002
    Posts
    4

    required fields in a form

    Hello,

    I'd like to know how to make certain text entry fields in a form mandatory. I can't seem to find the code anywhere.

    Thanks alot.
    San



  • #2
    Derek's Avatar
    Be Yourself

    Status
    Offline
    Join Date
    Aug 2001
    Location
    St. George, Utah, USA
    Posts
    466
    you could use this (if you have PHP enabled):

    PHP Code:
    <?php
    if ($form1 && $form2 && $form3) {
    echo 
    "Thank you for submitting your form";
    } else {
    echo 
    "You have not filled out all the required fields. Place hit your back button and fill out all the required fields.";
    }
    ?>
    or you could use javascript:

    Code:
    <script language="javascript">
    <!--
    var form1 = formname.inputname.value
    if (form1==""&&Submit=1) {
    alert("You have not filled out all the required fields.");
    }
    var form2 = formname.inputname2.value
    if (form2==""&&Submit=1) {
    alert("You have not filled out all the required fields.");
    }
    var form3 = formname.inputname3.value
    if (form3==""&&Submit=1) {
    alert("You have not filled out all the required fields.");
    }
    -->
    </script>
    I think that javascript one shoudl work but I just wrote that up so maybe it won't, check it out though.

  • #3
    Derek's Avatar
    Be Yourself

    Status
    Offline
    Join Date
    Aug 2001
    Location
    St. George, Utah, USA
    Posts
    466
    *fix*

    Code:
    <script language="javascript">
    <!--
    var form1 = formname.inputname1.value
    var form2 = formname.inputname2.value
    var form3 = formname.inputname3.value
    if (form1==""&&Submit=1) {
    alert("You have not filled out all the required fields.");
    }
    if (form2==""&&Submit=1) {
    alert("You have not filled out all the required fields.");
    }
    if (form3==""&&Submit=1) {
    alert("You have not filled out all the required fields.");
    } else {
    location.href="blah.html";
    }
    -->
    </script>

  • #4
    san's Avatar
    New User

    Status
    Offline
    Join Date
    Mar 2002
    Posts
    4

    Thumbs up

    whoa! that was quick. you guys rule. thanks!

  • #5
    Derek's Avatar
    Be Yourself

    Status
    Offline
    Join Date
    Aug 2001
    Location
    St. George, Utah, USA
    Posts
    466
    gee I love users who actually say thank you, a lot of them don't...

    your welcome

  • #6
    pegboy's Avatar
    Senior Member

    Status
    Offline
    Join Date
    Aug 2001
    Location
    Knoxville, TN
    Posts
    76
    i use a simple code for that. it's let's the CGI script do all the work. here's the HTML:

    <FORM METHOD=POST ACTION="cgi/bnbform.cgi">
    <INPUT TYPE="HIDDEN" NAME="required" VALUE="name,email,message">

    <INPUT TYPE="HIDDEN" NAME="data_order" VALUE="name,email,phone,subject,message">

    <INPUT TYPE="HIDDEN" NAME="submit_to" VALUE="contact@whatever.com">

    <INPUT TYPE="HIDDEN" NAME="form_id" VALUE="Contact from What Ever Dot Com">

    <INPUT TYPE="HIDDEN" NAME="ok_url" VALUE="http://www.whatever.com/thanks.htm">

    <INPUT TYPE="HIDDEN" NAME="not_ok_url" VALUE="http://www.whatever.com/oops.htm">

    you can find this CGI script at http://www.bignosebird.com hope that helps.
    robert koons

  • #7
    Derek's Avatar
    Be Yourself

    Status
    Offline
    Join Date
    Aug 2001
    Location
    St. George, Utah, USA
    Posts
    466
    actually that javascript checker didn't even work I just tested it cause I was bored and came up with a working one:

    Code:
    <html>
    
    <head>
    
    <script language="javascript">
    <!--
    function check() {
    if (document.daform.fname.value=="") {
    alert("You did not fill our your first name.");
    }
    if (document.daform.lname.value=="") {
    alert("You did not fill our your last name.");
    }
    if (document.daform.number.value=="") {
    alert("You did not fill our your number.");
    } else {
    location.href="blah2.html";
    }
    }
    -->
    </script>
    
    </head>
    
    <body>
    
    <form name="daform">
    First Name: <input type="text" size="15" name="fname">
    <br>
    Last Name: <input type="text" size="15" name="lname">
    <br>
    Number: <input type="text" size="15" name="number">
    <br>
    <input type="Button" value="Submit" name="Submit" onclick="check()">
    </form>
    
    </body>
    
    </html>

  • #8
    pegboy's Avatar
    Senior Member

    Status
    Offline
    Join Date
    Aug 2001
    Location
    Knoxville, TN
    Posts
    76
    what if you had an initial value set for the inputs. could you adjust the script to check for two options? like this maybe:

    <script language="javascript">
    <!--
    function check() {
    if (document.daform.fname.value=="enter your name here" "") {
    alert("You did not fill our your first name.");
    }
    if (document.daform.lname.value=="") {
    alert("You did not fill our your last name.");
    }
    if (document.daform.number.value=="") {
    alert("You did not fill our your number.");
    } else {
    location.href="blah2.html";
    }
    }
    -->
    </script>

    </head>

    <body>

    <form name="daform">
    <input type="text" size="15" name="fname" value="enter your name here">
    etc....
    robert koons

  • #9
    Derek's Avatar
    Be Yourself

    Status
    Offline
    Join Date
    Aug 2001
    Location
    St. George, Utah, USA
    Posts
    466
    you mean just adding this:

    Code:
    <script language="javascript">
    <!--
    function check() {
    if (document.daform.fname.value=="Enter Your First Name") {
    alert("You did not fill our your first name.");
    }
    if (document.daform.lname.value=="Enter Your Last Name") {
    alert("You did not fill our your last name.");
    }
    if (document.daform.number.value=="Enter Your Number") {
    alert("You did not fill our your number.");
    } else {
    location.href="blah2.html";
    }
    }
    -->
    </script>
    if this is what you want, yes, this would work.

  • #10
    pegboy's Avatar
    Senior Member

    Status
    Offline
    Join Date
    Aug 2001
    Location
    Knoxville, TN
    Posts
    76
    but will that still work if the field is empty?
    robert koons

  • #11
    Derek's Avatar
    Be Yourself

    Status
    Offline
    Join Date
    Aug 2001
    Location
    St. George, Utah, USA
    Posts
    466
    No, you'd have to add this:

    Code:
    <script language="javascript">
    <!--
    function check() {
    if (document.daform.fname.value=="Enter Your First Name") {
    alert("You did not fill our your first name.");
    }
    if (document.daform.fname.value=="") {
    alert("You did not fill our your first name.");
    }
    if (document.daform.lname.value=="Enter Your Last Name") {
    alert("You did not fill our your last name.");
    }if (document.daform.lname.value=="") {
    alert("You did not fill our your last name.");
    }
    if (document.daform.number.value=="Enter Your Number") {
    alert("You did not fill our your number.");
    }
    if (document.daform.number.value=="") {
    alert("You did not fill our your number.");
    } else {
    location.href="blah2.html";
    }
    }
    -->
    </script>
    Maybe there's a quicker way but I gotta get going. Tell me if any more problems arise.

  • #12
    Derek's Avatar
    Be Yourself

    Status
    Offline
    Join Date
    Aug 2001
    Location
    St. George, Utah, USA
    Posts
    466
    oh ya and be sure to make the form method=POST cause that fixes a little error that occurs if you play with the script you'd see it. Anyways later.

  • #13
    Derek's Avatar
    Be Yourself

    Status
    Offline
    Join Date
    Aug 2001
    Location
    St. George, Utah, USA
    Posts
    466
    lol sorry for dragging this on but there are little annoying (but can be costly) errors that I can't seam to fix so here's the code as it stands(if anybody cares):

    Code:
    <html>
    
    <head>
    
    <script language="javascript">
    <!--
    function check() {
    if (document.daform.fname.value=="") {
    alert("You must fill out ALL fields.");
    }
    else if (document.daform.lname.value=="") {
    alert("You must fill out ALL fields.");
    }
    else if (document.daform.number.value=="") {
    alert("You must fill out ALL fields.");
    } else {
    location.href="blah2.html";
    }
    }
    -->
    </script>
    
    </head>
    
    <body>
    
    <form name="daform">
    First Name: <input type="text" size="15" name="fname">
    <br>
    Last Name: <input type="text" size="15" name="lname">
    <br>
    Number: <input type="text" size="15" name="number">
    <br>
    <input type="Button" value="Submit" name="Submit" onclick="check()">
    </form>
    
    </body>
    
    </html>

  • #14
    QuietDean's Avatar
    Administrator

    Status
    Offline
    Join Date
    Oct 2000
    Location
    Bournemouth, UK
    Posts
    2,662
    When you say little errors, do you mean the fact that it doesn't actually submit any data?

    Here revised code that actually submits -

    Code:
    <html>
    
    <head>
    
    <script language="javascript">
    <!--
    function check() {
    if (document.daform.fname.value=="") {
    alert("You must fill out ALL fields.");
    return false;
    }
    else if (document.daform.lname.value=="") {
    alert("You must fill out ALL fields.");
    return false;
    }
    else if (document.daform.number.value=="") {
    alert("You must fill out ALL fields.");
    return false;
    } else {
    return true;
    
    }
    }
    -->
    </script>
    
    </head>
    
    <body>
    
    <form name="daform" onsubmit="return check()" action="blah2.php">
    First Name: <input type="text" size="15" name="fname">
    <br>
    Last Name: <input type="text" size="15" name="lname">
    <br>
    Number: <input type="text" size="15" name="number">
    <br>
    <input type="submit" value="Submit" name="Submit">
    </form>
    
    </body>
    
    </html>
    Was that the problem? Or something else?
    If one of our members helps you, please click the icon to add to their reputation!
    No support via email or private message - use the forums!
    Before you ask, have you Searched?

  • #15
    Derek's Avatar
    Be Yourself

    Status
    Offline
    Join Date
    Aug 2001
    Location
    St. George, Utah, USA
    Posts
    466
    actually (I shoudl have thought of that one lol) but that wasn't the problem. In the old code, if you only filled out say the number, it would only say ou didnt' fill out the first name, when you hadn't filled out the last name either. Same when you typed in only the last name, etc. It's with the "else if"'s and stuff that I can't seam to fix to make it say all the fields you haven't filled out. And when I left just the plain if's it said all the ones you hadn't filled out, but if you filled out the number at all it would go to the destination even if you hadn't filled out the first to, so the else if's change that, but then give me the second problem. Well if you can solve it you deserve a cookie I spent like 20 mintes messing aroudn with it but coudln't get it to work.



  • Page 1 of 2 12 Last

    Tags for this Thread

    Posting Permissions

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