Abundant Technologies - IT Consulting Experts

 Source Code Library

 Code Details

 
/************************************************************************************ '* Comments: Validate a time field hh:mm '* '* Parameters: oTimeVal - element to be evaluated '* '* Requirements: Used with onblur event to validate before submit '* '* Returns: Nothing, alerts and erases field '* '* Author: Tom Kershner, Abundant Technologies '************************************************************************************/ function isTime(oTimeVal){ var tArray; var hVal; var mVal; var pVal; var sSeparator; //Allow empty string, validate on submit if(oTimeVal.value=="") return true tArray = tSplit(oTimeVal.value, ":"); if (tArray.length == 3){ sSeparator=":" } else{ tArray = tSplit(oTimeVal.value, "-"); if (tArray.length == 3){ sSeparator="-" } else{ tArray = tSplit(oTimeVal.value, "."); if (tArray.length == 3){ sSeparator="." } else{ // invalid separator alert("1-Must enter a valid time. (hh:mm)"); oTimeVal.value=""; oTimeVal.focus(); return false; } } } // Assign values hVal = parseInt(tArray[0], 10); mVal = parseInt(tArray[1], 10); pVal = tArray[2]; // Make sure all the values are numbers if (isNaN(hVal) || isNaN(mVal)){ // invalid date alert("2-Must enter a valid time. (hh:mm)"); oTimeVal.value=""; oTimeVal.focus(); return false; } // Check minute value if (mVal > 59 || mVal < 0){ // invalid day value alert("3-Must enter a valid time. (hh:mm)"); oTimeVal.value=""; oTimeVal.focus(); return false; } // Check hour value if (hVal > 12 || hVal < 1){ // invalid month value alert("4-Must enter a valid time. (hh:mm)"); oTimeVal.value=""; oTimeVal.focus(); return false; } //add leading zero to minutes if (mVal<10){ mVal="0"+mVal; } oTimeVal.value=hVal+":"+mVal+" "+pVal; return true; } function tSplit(timeval, sepval){ var pos1; var pos2; var tArray; var foobar; foobar = new Array(0); tArray = new Array(3); // Get first separator position pos1 = timeval.indexOf(sepval, 0); // If it's the last char, it's bad if (pos1 == timeval.length - 1) return foobar; // Get AMPM separator position if (pos1 != -1) pos2 = timeval.indexOf(" ", pos1 + 1); else return foobar; if (pos2 != timeval.length - 1){ // no AMPM in string convert from mil time // Assign values into array if (pos1 != -1){ tArray[0] = timeval.substring(0, pos1); tArray[1] = timeval.substring(pos1 + 1,timeval.length + 1); if (tArray[0]<13){ tArray[2] = "AM"; } else{ tArray[0] = tArray[0]-12; tArray[2] = "PM"; } } else { return foobar; } } else{ // use entered AMPM value // Assign values into array if (pos1 != -1){ tArray[0] = timeval.substring(0, pos1); tArray[1] = timeval.substring(pos1 + 1,pos2); tArray[2] = timeval.substring(pos2 + 1,timeval.length + 1); } else { return foobar; } } return tArray; }

Mail To: info@3pc.com