[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 248
  • Last Modified:

Time validation

I am trying to check time..
When I used just for one time input text field, it works. But when I tried to use for-loop to check all time input text fields, my code did not work.
if I have more than one input field and check time correctly, how can I start??
Would you please help me ?

Thank you in advance
-------------------
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html>
<head>
<title>Untitled</title>

<style type="text/css">

.readOnly { color: black;}
.readWrite { color: #00CCFF; background-color:#00CCFF;}

</style>

<script language="javascript"><!--

function SetState( bool, targ, flag ) {
    targ.readOnly = !bool;
          targ.Disabled = !bool;

    if (flag) SetColor( bool, targ );
}

function SetColor( bool, targ ) {
    targ.className = (bool) ? 'readOnly': 'readWrite';

    <!--'readWrite' : -->
}


<!--time validation-->
function IsValidTime() {

var timeStrA = document.f.t2.value;
var timeStr = timeStrA2;
var timePat = /^(\d{1,2}):(\d{2})?$/;

var matchArray2  = timeStr.match(timePat);
var matchArray = matchArray2;

if (matchArray == null) {
alert("Time is not in a valid format.");
return false;
}

var hour = matchArray[1];
var minute = matchArray[2];

if (hour < 0  || hour > 24) {
alert("Hour must be between 1 and 12. (or 0 and 23 for military time)");
return false;
}

if (minute<0 || minute > 59) {
alert ("Minute must be between 0 and 59.");
return false;
}
}



//--></script>
</head>

<body>

<form name="f">
   <td><input type="checkbox" name="cb1" onclick="SetState(this.checked, document.forms['f'].t2, false);SetState(this.checked, document.forms['f'].t3, false);SetState(!this.checked, document.forms['f'].t1, true);SetState(!this.checked, document.forms['f'].t4, true);" /></td>
    <td>
    <select name="t1" class="readOnly" >
      <option>aa</option>
      <option>ab</option>
      <option>ac</option>
    </select>
    </td>
    <td><input type="text" name="t2"   readonly  /></td>
     <td><input type="text" name="t3"  readonly /></td>
    <td>
       <select name="t4" class="readOnly">
      <option>aa</option>
      <option>ab</option>
      <option>ac</option>
    </select>
      </td>
      <br />
      <td><input type="checkbox" name="cb21" onclick="SetState(this.checked, document.forms['f'].t21, false);SetState(this.checked, document.forms['f'].t31, false);SetState(!this.checked, document.forms['f'].t11, true);SetState(!this.checked, document.forms['f'].t41, true);" /></td>
    <td>
    <select name="t11" class="readOnly">
      <option>aa</option>
      <option>ab</option>
      <option>ac</option>
    </select>
    </td>
    <td><input type="text" name="t21"  readonly  /></td>
     <td><input type="text" name="t31"  readonly /></td>
    <td>
       <select name="t41" class="readOnly">
      <option>aa</option>
      <option>ab</option>
      <option>ac</option>
    </select>
      </td>
      <br />
      <td><input type="checkbox" name="cb12" onclick="SetState(this.checked, document.forms['f'].t22, false);SetState(this.checked, document.forms['f'].t32, false);SetState(!this.checked, document.forms['f'].t12, true);SetState(!this.checked, document.forms['f'].t42, true);" /></td>
    <td>
    <select name="t12" class="readOnly">
      <option>aa</option>
      <option>ab</option>
      <option>ac</option>
    </select>
    </td>
    <td><input type="text" name="t22"  readonly  /></td>
     <td><input type="text" name="t32" readonly /></td>
    <td>
       <select name="t42" class="readOnly">
      <option>aa</option>
      <option>ab</option>
      <option>ac</option>
    </select>
      </td>
      <br />
      <input type="submit" value="Submit" onClick="return IsValidTime();">

</form>

</body>
</html>
------------------------------
0
bbvic
Asked:
bbvic
1 Solution
 
SibishCommented:
Hi,

From the question you have asked what is clear is that you want to check the time values in all the text boxes. You can write this piece of code in your time validating function.

for(i=0;i<document.f.elements.length;i++)  //f is the form name
    if(document.f.elements[i].type=="text"){  //checking the type of control  inorder to
                                                              // avoid other controls in the form
       var value=document.f.elements[i].value; // gets the value of the text
       //write your time validation code
    }//end of if
}//end of for


Regards,
Sibish.K.Abraham
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now