www.webdeveloper.com
Results 1 to 8 of 8

Thread: help with project - desparate.

  1. #1
    Join Date
    Apr 2012
    Posts
    7

    help with project - desparate.

    i am new to coding and to be honest i am struggling not alittle but quite alot. I am working on a project and just cannot get my script to work.

    Basically there are eight fields and they must do the following:

    1. RunnerID. This is a unique number assigned to a runner when they join parkrun which is used to identify the runner. A runnerID is between 1 and 99999, as numbers with up to five digits are allowed.
    2. EventID. This is a unique number assigned to an event. This has the same format as ‘RunnerID’.
    3. Date. This is the date of the event (and result). A date has the format YYYY-MM-DD.
    4. FinishTime. This is the time taken by the runner to complete the event. A time has the format HH:MM:SS.
    5. Position. This is the runner’s finishing position. The finishing number is within the range 1-10000.
    6. CategoryID. This is a classification of the runner by age and gender. A category is a number in the range 1-100.
    7. AgeGrade. This is a calculated number which “Gives an indication of how well the runner has done relative to their age and gender”. The format of this is a percentage with two decimal places, for example 99.36 or 13.45.
    8. PB (personal best). This indicates if the time for the event ‘FinishTime’ represents this runner’s best ever time. It has the format ‘1’ representing ‘true’ or ‘0’ (i.e. ‘false’).


    I have tried to adjust several scripts from the internet to make it work, but unforunately, it doesn't work for me. Please help if you can.


    zak:

  2. #2
    Join Date
    Apr 2006
    Location
    Houston
    Posts
    1,374
    You have described 8 form fields, but not what you want done with them or what you need javascript for.

    Are you wanting a function that validates that the input is in the correct format ?

    School homework project ?

  3. #3
    Join Date
    Apr 2012
    Posts
    7
    this is what i have tried to do but i can't even get the first field to validate. i need some training.

    !DOCTYPE html PUBLIC
    "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
    <meta http-equiv="Content-type" content="text/html; charset=iso-8859-1"/>
    <title>Submit runner time</title>
    <script type="text/javascrip>

    var ck_RunnerID = /^([0-9]|[1-9][0-9]|[1-9][0-9][0-9]|[1-9][0-9][0-9][0-9]|[1-9][0-9][0-9][0-9][0-9])$/;
    var ck_EventID = /^([0-9]|[1-9][0-9]|[1-9][0-9][0-9]|[1-9][0-9][0-9][0-9]|[1-9][0-9][0-9][0-9][0-9])$/;
    var ck_NewDate =
    var ck_FinishTime =
    var ck_Position =
    var ck_CategoryID =
    var ck_AgeGrade =
    var ck_PB =



    function validate(form){
    var RunnerID = form.RunnerID.value;
    var EventID = form.EventID.value;
    var NewDate = form.NewDate.value;
    var FinishTime = form.FinishTime.value;
    var Position = form.Position.value;
    var CategoryID = form.CategoryID.value;
    var AgeGrade = form.AgeGrade.value;
    var PB = form.PB.value;

    var errors = [];

    if (!ck_RunnerID.test(RunnerID)) {
    errors[errors.length] = "RunnerID.";
    }

    if (!ck_EventID.test(EventID)) {
    errors[errors.length] = "You must enter a valid Event ID.";
    }
    if (!ck_NewDate.test(NewDate)) {
    errors[errors.length] = "You must enter a date.";
    }

    if (!ck_FinishTime.test(FinishTime)) {
    errors[errors.length] = "You must enter a finish time.";

    }
    if (!ck_Position.test(Position)) {
    errors[errors.length] = "Please your position.";
    }

    if (!ck_CategoryID.test(CategoryID)) {
    errors[errors.length] = "You must enter a valid Category ID.";
    }
    if (!ck_AgeGrade.test(AgeGrade)) {
    errors[errors.length] = "You must enter your Age Grade.";
    }

    if (!ck_PB.test(PB)) {
    errors[errors.length] = "Is this your personal best time.";
    if (errors.length > 0) {
    reportErrors(errors);
    return false;
    }

    return true;
    }

    function reportErrors(errors){
    var msg = "Please Enter Valide Data...\n";
    }
    alert(msg);
    }

    </script>
    </head>
    <body>
    <hr/>
    <h1>Submit runner time</h1>
    <hr/>
    Note: all fields are mandatory.
    <p/>
    <form autocomplete="off"
    <form id="feedback" action="http://as5676.tt284.open.ac.uk/files/storedata.php"
    method="post" name="submitrunnertime" enctype="multipart/form-data" enctype="multipart/form-data" >


    <table>
    <tr><td>Runner ID</td>
    <td><input type="text" name="RunnerID" size="5" maxlength="5"/></td>
    </tr>
    <tr><td>Event ID</td>
    <td><input type="text" name="EventID" size="5" maxlength="5"/></td>
    </tr>
    <tr><td>new Date (YYYY-MM-DD):</td>
    <td><input type="text" name="txtDate" size="10" maxlength="10"/></td>
    </tr>
    <tr><td>Finish time (HH:MM:SS)</td>
    <td><input type="text" name="FinishTime" size="8" maxlength="8"/></td>
    </tr>
    <tr><td>Position</td>
    <td><input type="text" name="Position" size="5" maxlength="5"/></td>
    </tr>
    <tr><td>Category ID</td>
    <td><input type="text" name="CategoryID" size="3" maxlength="3"/></td>
    </tr>
    <tr><td>Age grade</td>
    <td><input type="text" name="AgeGrade" size="5" maxlength="5"/></td>
    </tr>
    <tr><td>Personal best</td>
    <td><input type="text" name="PB" size="1" maxlength="1"/></td>
    </tr>
    </table>
    <p><input type="submit" value="Submit" onSubmit="return validate(this)"; name="form" />
    <hr/>
    </form>
    </body>
    </html>

  4. #4
    Join Date
    Apr 2012
    Posts
    7
    i am trying to validate the 8 fields, using function. i have spent 5 days using various codes off the internet, but i cant get any to work. i need some help, yes it is part of a project.

    !DOCTYPE html PUBLIC
    "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
    <meta http-equiv="Content-type" content="text/html; charset=iso-8859-1"/>
    <title>Submit runner time</title>
    <script type="text/javascrip>

    var ck_RunnerID = /^([0-9]|[1-9][0-9]|[1-9][0-9][0-9]|[1-9][0-9][0-9][0-9]|[1-9][0-9][0-9][0-9][0-9])$/;
    var ck_EventID = /^([0-9]|[1-9][0-9]|[1-9][0-9][0-9]|[1-9][0-9][0-9][0-9]|[1-9][0-9][0-9][0-9][0-9])$/;
    var ck_NewDate =
    var ck_FinishTime =
    var ck_Position =
    var ck_CategoryID =
    var ck_AgeGrade =
    var ck_PB =



    function validate(form){
    var RunnerID = form.RunnerID.value;
    var EventID = form.EventID.value;
    var NewDate = form.NewDate.value;
    var FinishTime = form.FinishTime.value;
    var Position = form.Position.value;
    var CategoryID = form.CategoryID.value;
    var AgeGrade = form.AgeGrade.value;
    var PB = form.PB.value;

    var errors = [];

    if (!ck_RunnerID.test(RunnerID)) {
    errors[errors.length] = "RunnerID.";
    }

    if (!ck_EventID.test(EventID)) {
    errors[errors.length] = "You must enter a valid Event ID.";
    }
    if (!ck_NewDate.test(NewDate)) {
    errors[errors.length] = "You must enter a date.";
    }

    if (!ck_FinishTime.test(FinishTime)) {
    errors[errors.length] = "You must enter a finish time.";

    }
    if (!ck_Position.test(Position)) {
    errors[errors.length] = "Please your position.";
    }

    if (!ck_CategoryID.test(CategoryID)) {
    errors[errors.length] = "You must enter a valid Category ID.";
    }
    if (!ck_AgeGrade.test(AgeGrade)) {
    errors[errors.length] = "You must enter your Age Grade.";
    }

    if (!ck_PB.test(PB)) {
    errors[errors.length] = "Is this your personal best time.";
    if (errors.length > 0) {
    reportErrors(errors);
    return false;
    }

    return true;
    }

    function reportErrors(errors){
    var msg = "Please Enter Valide Data...\n";
    }
    alert(msg);
    }

    </script>
    </head>
    <body>
    <hr/>
    <h1>Submit runner time</h1>
    <hr/>
    Note: all fields are mandatory.
    <p/>
    <form autocomplete="off"
    <form id="feedback" action="http://as5676.tt284.open.ac.uk/files/storedata.php"
    method="post" name="submitrunnertime" enctype="multipart/form-data" enctype="multipart/form-data" >


    <table>
    <tr><td>Runner ID</td>
    <td><input type="text" name="RunnerID" size="5" maxlength="5"/></td>
    </tr>
    <tr><td>Event ID</td>
    <td><input type="text" name="EventID" size="5" maxlength="5"/></td>
    </tr>
    <tr><td>new Date (YYYY-MM-DD):</td>
    <td><input type="text" name="txtDate" size="10" maxlength="10"/></td>
    </tr>
    <tr><td>Finish time (HH:MM:SS)</td>
    <td><input type="text" name="FinishTime" size="8" maxlength="8"/></td>
    </tr>
    <tr><td>Position</td>
    <td><input type="text" name="Position" size="5" maxlength="5"/></td>
    </tr>
    <tr><td>Category ID</td>
    <td><input type="text" name="CategoryID" size="3" maxlength="3"/></td>
    </tr>
    <tr><td>Age grade</td>
    <td><input type="text" name="AgeGrade" size="5" maxlength="5"/></td>
    </tr>
    <tr><td>Personal best</td>
    <td><input type="text" name="PB" size="1" maxlength="1"/></td>
    </tr>
    </table>
    <p><input type="submit" value="Submit" onSubmit="return validate(this)"; name="form" />
    <hr/>
    </form>
    </body>
    </html>

  5. #5
    Join Date
    Apr 2012
    Posts
    13
    if the highest number is 99999 you cant put a higher number in because your maxlength="5" for runner id to get an error message you would have to put a minus before any number you type.
    your <form> is all wrong aswell i think.
    Last edited by stevenn21; 04-27-2012 at 01:33 PM.

  6. #6
    Join Date
    May 2006
    Location
    Somewhere behind your screen
    Posts
    1,670
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
    <meta http-equiv="Content-type" content="text/html; charset=iso-8859-1" />
    <title>Submit runner time</title>
    <style type="text/css">
    body{text-align:center;font-family:Verdana,Arial;font-size:11px;background-color:#fff;color:#000;}
    input[type="text"]{text-align:center;width:50px;}
    td.header{font-weight:bold;}
    </style>
    <script type="text/javascript">
    var path_RunnerID = /^[\d]{1,5}$/;
    var path_EventID = /^[\d]{1,5}$/;
    var path_NewDate = /^[\d]{4}\-[\d]{2}\-[\d]{2}$/;
    var path_FinishTime = /^[\d]{2}\:[\d]{2}\:[\d]{2}$/;
    var path_Position = /^[\d]{1,5}$/;
    var path_CategoryID = /^[\d]{1,3}$/;
    var path_AgeGrade = /^[\d]{1,2}\.[\d]{2}$/;
    var path_PB = /^[01]{1}$/;
    
    function validate(form){
    var valid = true;
    var elems = form.elements;
    // first we check all the fields format
    for(var i = 0; i < elems.length; i++){
    // if the current field type is not 'text' let the loop skip it (buttons or some other elements meant)
    if(elems[i].type != 'text'){continue;}
    else{
    var path = eval('path_'+elems[i].name);
    var val = parseInt(elems[i].value);
    if(path.test(elems[i].value) == false){alert('Wrong format at the '+elems[i].name+' field!');elems[i].value = '';elems[i].focus();valid = false;break;}
    /* if everything seems good we check the field again 
    see the example for RunnerID and EventID fields below and check every other field logically.
    for the date field it means that:
    - year must be equal to the current year
    - month must be between 1 and 12
    - day must be between 1 and 31 
    etc.
    */
    if(elems[i].name == 'RunnerID' || elems[i].name == 'EventID'){
    if(val < 1 || val > 99999){alert('Wrong value at the '+elems[i].name+' field!');elems[i].value = '';elems[i].focus();valid = false;break;}
    }
    }
    }
    /*
    if all checks done and passed the 'valid' variable will remain TRUE 
    and we submit the form only if it is still TRUE:
    */
    if(valid == true){form.submit();}
    }
    </script>
    </head>
    <body>
    <hr />
    <h1>Submit runner time</h1>
    <hr />
    <p>Note: all fields are mandatory.</p>
    <form id="feedback" action="http://as5676.tt284.open.ac.uk/files/storedata.php" method="post" name="submitrunnertime" enctype="multipart/form-data" enctype="multipart/form-data">
    <table align="center" width="50&#37;" cellpadding="2" cellspacing="2" border="0">
    <tbody>
    <tr>
    <td width="15%" class="header">Runner ID</td>
    <td width="15%"><input type="text" name="RunnerID" size="5" maxlength="5" /></td>
    <td>(between 1 and 99999, as numbers with up to five digits are allowed)</td>
    </tr>
    <tr>
    <td class="header">Event ID</td>
    <td><input type="text" name="EventID" size="5" maxlength="5" /></td>
    <td>(between 1 and 99999, as numbers with up to five digits are allowed)</td>
    </tr>
    <tr>
    <td class="header">new Date</td>
    <td><input type="text" name="NewDate" size="10" maxlength="10" /></td>
    <td>(YYYY-MM-DD)</td>
    </tr>
    <tr>
    <td class="header">Finish time</td>
    <td><input type="text" name="FinishTime" size="8" maxlength="8" /></td>
    <td>(HH:MM:SS)</td>
    </tr>
    <tr>
    <td class="header">Position</td>
    <td><input type="text" name="Position" size="5" maxlength="5" /></td>
    <td>(a number within the range 1-10000)</td>
    </tr>
    <tr>
    <td class="header">Category ID</td>
    <td><input type="text" name="CategoryID" size="3" maxlength="3" /></td>
    <td>(a number in the range 1-100)</td>
    </tr>
    <tr>
    <td class="header">Age grade</td>
    <td><input type="text" name="AgeGrade" size="5" maxlength="5" /></td>
    <td>(percentage with two decimal places)</td>
    </tr>
    <tr>
    <td class="header">Personal best</td><td><input type="text" name="PB" size="1" maxlength="1" /></td>
    <td>(1 or 0)</td>
    </tr>
    </tbody>
    </table>
    <p><input type="button" value="Submit" onclick="validate(this.form)"; name="checkit" /></p>
    <hr />
    </form>
    </body>
    </html>

  7. #7
    Join Date
    Apr 2012
    Posts
    13

    Padonak your a genius

    all hale Padonak thats some mean scripting i wish to be as good as you someday

  8. #8
    Join Date
    May 2006
    Location
    Somewhere behind your screen
    Posts
    1,670
    steven, watch the brackets <wink>

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles