Results 1 to 2 of 2

Thread: Time Validation

  1. #1
    Join Date
    Feb 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Time Validation

    Hi, can you please help?! I’m new to Javascript and have only experience in classic ASP and so need some help!

    Basically I’m trying to create some validation code to check the time enter by the user into four fields for time recording purposes. Those fields being; hour start, minute start, hour finish and minute finish. So for example the user could enter 10:04 til 11:15. The reason the time is split this way into fields is the hour field is drop down menu list specific start and finish hours. The minute field, is manual entry field, for both start and finish minutes. This field should only allow numeric characters only and a maximum of 59 minutes to be entered.

    This is what I’ve come up with so far… how it fails if you enter minute value of say 08. Please help… I’m about to pull out my remaining hair! Thanks.
    <SCRIPT LANGUAGE="JavaScript">
    <!--
    function validfield(field,fieldname) {
    if (field.value > "")
    {

    var userAge = parseInt(field.value);
    if (!userAge) {
    if (field.value=="00")
    {
    return true;
    }
    else
    {
    alert("You must enter minute value in "+fieldname+" field");
    return false;
    }
    } else if (userAge > 59) {
    alert("Minute Value Greater Than 59 in the "+fieldname+" field");
    return false;
    } else {

    return true;
    }
    }
    return true;
    }

    function valid(form){
    var result = true;
    result = result && validfield(form.MSMM,"Morning Start");
    result = result && validfield(form.LSMM,"Lunch Start");
    result = result && validfield(form.LEMM,"Lunch End");
    result = result && validfield(form.AEMM,"Afternoon End");
    return result
    }

    // -->
    </SCRIPT>

  2. #2
    Join Date
    Dec 2008
    Location
    Portsmouth, UK
    Posts
    1,891
    Thanks
    2
    Thanked 441 Times in 435 Posts

    Default

    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
        "http://www.w3.org/TR/html4/loose.dtd">
    
    <html>
    
    <head>
      <title></title>
    <SCRIPT LANGUAGE="JavaScript">
    <!--
    function validfield(h,m,hname) {
     var mess=[];
     if (!Number(h.value)){
       mess.push("You must enter hour value in "+hname+" h");
     }
     if (!Number(m.value)||m.value>59){
      mess.push("You must enter minute value in "+hname+" h");
     }
     if (mess.length==0) return (h.value*1+'.'+m.value*1);
     alert(mess.join('\n'));
     return false;
    }
    
    function Number(nu){
     return /(^-?\d\d*\.\d*$)|(^-?\d\d*$)|(^-?\.\d\d*$)/.test(nu);
    }
    
    function valid(form){
     var nams=['Morning Start','Lunch Start','Lunch End','Afternoon End'];
     var rtn=[];
     rtn.push(validfield(form.MSH,form.MSM,nams[0]));
     rtn.push(validfield(form.LSH,form.LSM,nams[1]));
     rtn.push(validfield(form.LEH,form.LEM,nams[2]));
     rtn.push(validfield(form.AEH,form.AEM,nams[3]));
     for (var mess=[],z0=0;z0<rtn.length;z0++){
      if (!rtn[z0]) return false;
      if (rtn[z0-1]&&rtn[z0]<=rtn[z0-1]) mess.push(nams[z0]+ ' must be greater than '+nams[z0-1] )
     }
     if (mess.length>0){
      alert(mess.join('\n'));
      return false;
     }
     return true;
    }
    
    // -->
    </SCRIPT>
    </head>
    
    <body>
    <form >
    Morning Start <input name="MSH" size="5"> <input name="MSM" size="5"> <br>
    Lunch Start <input name="LSH" size="5"> <input name="LSM" size="5"> <br>
    Lunch End <input name="LEH" size="5"> <input name="LEM" size="5"><br>
    Afternoon End <input name="AEH" size="5"> <input name="AEM" size="5"><br>
    <input type="button" name="" value="TEST" onclick="valid(this.form);">
    </form>
    </body>
    
    </html>

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
  •