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
    Regular Coder
    Join Date
    Apr 2004
    Posts
    684
    Thanks
    24
    Thanked 1 Time in 1 Post

    Required form fields

    Say I have a form & I want to require people fill in form fields on the form & to have a alert box come up if they didn't fill one in (when they click the submit button) how can I do this?

    Just say the form field names that are required are..

    name
    email


    Cheers!

  • #2
    Banned
    Join Date
    Sep 2003
    Posts
    3,620
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Forum Search: keywords=required form fields.....

  • #3
    Regular Coder
    Join Date
    Aug 2002
    Location
    USA
    Posts
    625
    Thanks
    0
    Thanked 0 Times in 0 Posts
    <script>
    <!--
    function checkForm(){
    width=400;
    height=300;
    if (document.myForm.cbox.checked){
    window.open('http://www.yourDomain.com/yourPage.html','yourWinName','width=400,height=300,top='+((screen.height-height)/2)+',left='+((screen.width-width)/2));
    }else{
    alert("You Must Agree To The Terms To Continue");
    }
    }
    //-->
    </script>




    <form name="myForm" method="post" onsubmit="checkForm();return false;">
    <textarea col="15" rows="2" wrap="virtual">Your Terms Go Here...</textarea>
    <input type="checkbox" name="cbox" checked />
    <input type="submit" name="myButton" value="Agree" />
    </form>



    -james
    "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. "

  • #4
    Regular Coder
    Join Date
    Apr 2004
    Posts
    684
    Thanks
    24
    Thanked 1 Time in 1 Post
    Did the search willy but couldn't really find anything suitable... seemed to just find people trying to fix their own code.

    Thanks James.. but I need something that just checks to make sure they have filled in all requiref fields & if not pops an alert box, otherwise it continues with the submit action.

  • #5
    Regular Coder
    Join Date
    Jul 2002
    Posts
    698
    Thanks
    0
    Thanked 0 Times in 0 Posts
    this only tested in IE but should work in Firefox to. basicly put the onclick event on the submit button and then the script in the head and write required in all the tags that are required.

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

    <html>
    <head>
    <title>Untitled</title>
    <script>
    function tes(aForm){
    var faults=new Array();
    var fields=aForm.getElementsByTagName('input');
    for(I=0;I<fields.length;I++){
    if(fields[I].getAttribute('required'))
    {
    if(fields[I].value.replace(" ","")=="")
    {
    faults[faults.length]=fields[I].name+' field must be filled';
    }
    }
    }
    if(aForm.test.value.replace(" ","")=="")
    {
    faults[faults.length]='test field must be filled';
    }
    if(aForm.test1.value.replace(" ","")=="")
    {
    faults[faults.length]='test1 field must be filled';
    }

    if(faults.length>0){
    alert(faults.join('\n\r'));
    return false
    }

    return true;
    }
    </script>
    </head>

    <body>
    <form>
    <input type="text" name="test" required>
    <input type="text" name="test1" required>
    <input type="submit" onclick="return tes(this.form);" value="click">
    </form>


    </body>
    </html>

  • #6
    Regular Coder
    Join Date
    Apr 2004
    Posts
    684
    Thanks
    24
    Thanked 1 Time in 1 Post
    Thanks for that... I'll test it out!


  •  

    Posting Permissions

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