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 3 of 3

Thread: Checking a Form

  1. #1
    New to the CF scene
    Join Date
    May 2005
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Checking a Form

    Hello, I was wondering if anyone could help me use JavaScript to have a form be checked. I've already created the form and the program knows how to check that someone has clicked a radio button, check box, and selected something from a drop down list, but I still need to figure out how to make it check if there is something written in each of the text boxes. Thanks a lot, I really appreciate it. Here is my program so far:

    <html>
    <head>
    <title>Item Selection</title>
    </head>

    <body bgcolor="#FFFFFF" text="#000000" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">

    <SCRIPT LANGUAGE="JavaScript">
    // good holds the number of checks the form passes, it must pass three to be submitted
    var good = 0

    var totalpri=0, totalsecond=0, count = 0
    var maxpri, maxsecond
    courses = new Array()



    function count_pri(form) {

    var total = 0

    for (var i=0; i < form.pri.length; i++) {
    if (form.pri[i].checked) {
    total = total + 1
    courses[count] = form.pri[i].value
    count = count + 1
    } // end if
    } //end for

    if (total == 0 ) {
    alert ("You must pick a Primary Project Choice.")
    }
    else {
    good = good + 1
    }
    } // end count_pri


    function count_second(form) {

    var total = 0

    for (var i=0; i < form.second.length; i++) {
    if (form.second[i].checked) {
    total = total + 1
    courses[count] = form.second[i].value
    count = count + 1
    } // end if
    } //end for

    if (total == 0 ) {
    alert ("You must pick at least one Secondary Project Choice.")
    }
    else {
    good = good + 1
    }
    } // end count_pri

    function check_house (form) {

    var dble = 0
    var nohouse = 0

    var index1 = form.s1.selectedIndex
    var houses = form.s1.options[index1].text

    if (houses.length > 0) {
    nohouse = 1
    }
    else {
    alert("You have not chosen an House.")
    }

    if (dble == 0 && nohouse == 1) {
    good = good + 1
    }
    } // end check_house

    function check_name (form) {

    var namey = 0
    var noname = 0

    var index2 = form.textfield.selectedIndex
    var names = form.textfield.options[index2].text

    if (names.length > 0) {
    noname = 1
    }
    else {
    alert("You have not entered your Name!")
    }

    if (namey == 0 && noname == 1) {
    good = good + 1
    }
    }
    // end check_name
    //if (element_type == "text") {
    //if (element_value.length == 0) {
    //alertstr += "Form element '" + element_name + "' contains no data.\n\n";
    //allvalid = false;

    function check_choices(form) {

    good = 0
    count = 0
    maxpri = form.pri.length
    maxsecond = form.second.length

    count_pri(form)
    if (good == 1) {
    count_second (form)
    }
    if (good == 2) {
    check_house(form)
    }
    if (good == 3) {
    check_name(form)
    }
    if (good == 4) {
    form.submit()
    }
    else {
    //form.reset()
    }
    alert("Student Name: ")
    } //end check_choices
    </SCRIPT>
    <p><b style="font-size:16px">TIK20 Summativew Assessment Project </b></p>
    <form name="form1" method="get" action="">
    <p><strong>Student Name: </strong>
    <INPUT name="textfield">
    <br>
    <br>
    <b style="font-size:15px">House:</b>&nbsp;&nbsp;
    <select name="s1">
    <option value=" "></option>
    <option value="Cartier">Cartier</option>
    <option value="Hudson">Hudson</option>
    <option value="Mackenzie">Mackenzie</option>
    <option value="Massie">Massie</option>
    <option value="Simcoe">Simcoe</option>
    <option value="Wolfe">Wolfe</option>
    </select>
    <strong>Year</strong>:
    <input maxlength=1 size=6
    name=textfield2>
    </p>

    <table cellpadding="3" width="663" cellspacing="0">

    <tr>
    <td valign="top" width="253"><p><STRONG>Project Primary Choice:</STRONG>
    <table border="1" cellpadding="3" cellspacing="0" bordercolor="#003322" width="222">
    <tr>
    <td><INPUT type=radio
    value="Operating Systems" name="pri">
    Operating Systems </td>
    </tr>
    <tr>
    <td><LABEL>
    <INPUT type=radio value="Personal Computers" name="pri">
    Personal Computers</LABEL></td>
    </tr>
    <tr>
    <td><LABEL>
    <INPUT type=radio value=Servers name="pri">
    Servers</LABEL></td>
    </tr>
    <tr>
    <td><LABEL>
    <INPUT type=radio value="Job Trends" name="pri">
    Job Trends</LABEL></td>
    </tr>
    <tr>
    <td><LABEL>
    <INPUT type=radio value="Personal Devices"
    name="pri">
    Personal Devices</LABEL></td>
    </tr>
    <tr>
    <td><LABEL>
    <INPUT type=radio
    value=Networks name="pri">
    Networks</LABEL></td>
    </tr>
    <tr>
    <td><LABEL>
    <INPUT type=radio
    value="Games, Game Devices" name="pri">
    Games, Game Devices</LABEL></td>
    </tr>
    <tr>
    <td><LABEL>
    <INPUT type=radio value=Software name="pri">
    Software</LABEL></td>
    </tr>
    </table>
    </td>
    <td valign="top" width="396"><p><STRONG>Project Secondary Choices: <BR>
    (Choose 1 to 3 of the below):</STRONG>
    <table width="305" border="1" cellpadding="3" cellspacing="0" bordercolor="#003322">
    <tr>
    <td width="400"><INPUT type=checkbox value=checkbox name="second">
    Operating Systems</td>
    </tr>
    <tr>
    <td width="400"><input type=checkbox value=checkbox name="second">
    Personal Computers</td>
    </tr>
    <tr>
    <td width="400"><input type=checkbox value=checkbox name="second">
    Servers</td>
    </tr>
    <tr>
    <td width="400"><input type=checkbox value=checkbox name="second">
    Job Trends</td>
    </tr>
    <tr>
    <td><input type=checkbox value=checkbox name="second">
    Personal Devices</td>
    </tr>
    <tr>
    <td><input type=checkbox value=checkbox name="second">
    Networks</td>
    </tr>
    <tr>
    <td><input type=checkbox value=checkbox name="second">
    Game, Game Devices</td>
    </tr>
    <tr>
    <td><input type=checkbox value=checkbox name="second">
    Software </td>
    </tr>
    </table>
    </td>
    </tr>
    <tr>

    <td colspan="2"> <p><b style="font-size:15px"><br>
    </b></p>
    <blockquote>
    <p>&nbsp; </p>
    </blockquote>
    <p>
    <input type="button" name="check" value="Submit Selections" onClick="check_choices(this.form)">
    </p>

    </td>
    </tr>
    </table>
    </form>

    <p>&nbsp;</p>
    </body>
    </html>

  • #2
    Senior Coder
    Join Date
    Mar 2005
    Location
    Portsmouth UK
    Posts
    4,505
    Thanks
    3
    Thanked 501 Times in 488 Posts
    if (document.*FormName*.*TEXTINPUTName*.value.length<1){
    alert('Buck UP!')
    }

    *ccc* = your names

  • #3
    New to the CF scene
    Join Date
    May 2005
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thanks!


  •  

    Posting Permissions

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