SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Hybrid View

  1. #1
    SitePoint Zealot litmania_com's Avatar
    Join Date
    Feb 2001
    Location
    London
    Posts
    186
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How do I make it compulsory to complete all fields in a form?

    How do I make it compulsory to complete all fields in an ezine sign-up form?

    See my form here.

    If you try to fill in the form, you will see that it lets you complete the procedure without filling in all the sections (no name, and for many subscribers the country value="Select Country" ).

    Is there anyway to make it obligatory for somebody to fill in all the fields? How can we make all the fields, including name and country, compulsory?

    Thank you very much for your help. I appreciate you taking the time to advise me
    Nadeem Azam
    Litmania.com Inc.

    Free promotion - check out this 1:1 banner exchange
    Dot com domains - the world's most prestigious domain names for sale

  2. #2
    We like music. weirdbeardmt's Avatar
    Join Date
    May 2001
    Location
    Channel Islands Girth: Footlong
    Posts
    5,882
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It depends what language you are using, cos there are lots of different ways to do it. Browsing your code it looks like PHP, so you can do this:
    PHP Code:

    if (!isset($submit)):

    ?>

    [code for your form]

    <?php

    else:

    if (
    $email=="" or $name=="" or $sex=="" or $country==""

    {

        
    error("One or more required fields were left blank.\\n".              
            
    "Please fill them in and try again.");    

    }
    This will only work however if you are using php_self as your form action. There are also ways of checking with Javascript, or you can add:

    Code:
    <INPUT TYPE="hidden" NAME="REQUIRED" VALUE="email, name, sex, country">
    I swear to drunk I'm not God.
    Matt's debating is not a crime
    Hint: Don't buy a stupid dwarf Clicky

  3. #3
    SitePoint Guru Vincent Puglia's Avatar
    Join Date
    Feb 2000
    Location
    where the World once stood
    Posts
    700
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    The only proper way to make all fields required is to validate each field within the form. For example:

    function validateForm(formObj)
    {
    for (i=0; i< formObj.length; i++)
    {
    if (formObj.elements[i].type == 'text')
    if (formObj.elements[i].length == 0)
    alert("you must enter something in " + formObj.elements[i].name + " field.")
    ..........
    }
    }


    See the "Validating Forms" script/tutorial at my site; it is more of a how-to than a here-is.

    Vinny
    Where the World Once Stood
    the blades of grass
    cut me still

  4. #4
    SitePoint Zealot litmania_com's Avatar
    Join Date
    Feb 2001
    Location
    London
    Posts
    186
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you very much for your help Vincent and 'weirdbeardmt'. I appreciate you taking the time and trouble to assist me.

    I've been to Vinny's site, but the coding there is well beyond my grasp. I gave up after half an hour trying to understand it

    Is there no simpler way of not allowing the surfer to complete the sign-up procedure unless they enter their name and country?
    Nadeem Azam
    Litmania.com Inc.

    Free promotion - check out this 1:1 banner exchange
    Dot com domains - the world's most prestigious domain names for sale

  5. #5
    We like music. weirdbeardmt's Avatar
    Join Date
    May 2001
    Location
    Channel Islands Girth: Footlong
    Posts
    5,882
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is quite basic, but it will work (courtesy of www.javascriptsource.com):

    PHP Code:
    <SCRIPT LANGUAGE="JavaScript">

    function 
    checkrequired(which) {
    var 
    pass=true;
    if (
    document.images) {
    for (
    i=0;i<which.length;i++) {
    var 
    tempobj=which.elements[i];
    if (
    tempobj.name.substring(0,8)=="required") {
    if (((
    tempobj.type=="text"||tempobj.type=="textarea")&&
    tempobj.value=='')||(tempobj.type.toString().charAt(0)=="s"&&
    tempobj.selectedIndex==0)) {
    pass=false;
    break;
             }
          }
       }
    }
    if (!
    pass) {
    shortFieldName=tempobj.name.substring(8,30).toUpperCase();
    alert("Please make sure the "+shortFieldName+" field was properly completed.");
    return 
    false;
    }
    else
    return 
    true;
    }

    </script> 
    That goes in the head, then this for your form:

    Code:
    <form onSubmit="return checkrequired(this)">
    <input type="text" name="requiredname">
    <input type="text" name="requiredemail">
    <input type=submit value="Submit">
    </form>
    You can add as many fields as you want, just place required in the name section.
    I swear to drunk I'm not God.
    Matt's debating is not a crime
    Hint: Don't buy a stupid dwarf Clicky


Bookmarks

Posting Permissions

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