Wednesday, June 12, 2013

Time Validation in Java Script

This function validate the time format in 24 hours. 
function timeValidation(strTime) {
    if (strTime.indexOf(':') == -1 || strTime.length < 4 || strTime.length > 5) {
        return false;
    }
    var n = strTime.split(":");
    if (n.length > 2) {
        return false;
    }

    if (isNaN(n[0]) || n[0] > 23) {
        return false;
    }
    var minutePart = n[1].substring(0, 2);

    if (minutePart.length!=2 || isNaN(minutePart) || minutePart > 59) {
        return false;
    }    
    return true;

}