validation for 12 hours time with AM/PM using java script in PHP

javascript, php, regex

Solution

Try this

function testTime( time ) {
  var regex = /^([0-1][0-9])\:[0-5][0-9]\s*[ap]m$/i;
  var match = time.match( regex );
  if ( match ) {
    var hour  = parseInt( match[1] );
    if ( !isNaN( hour) && hour <= 11 ) {
      return true;
    }
  }
  return false;
}

testTime( '12:00 AM' ); // false  
testTime( '11:59 PM' ); // true  
testTime( '00:00 AM' ); // true  
testTime( '00:00am' ); // true  
testTime( '10:00pm' ); // true  

Problem

I am trying to validate the time in ``` 00:00 to 11:59 ends with AM OR PM ``` I was tring some regex,but not getting succesful to validate time. My java script function is ``` function verifydata( incoming ) { var re = (1[012]|[1-9]):[0-5][0-9](\\s)?(?i)(am|pm); if(incoming.time.value != '' && !incoming.time.value.match(re)) { alert("Invalid time format: " + incoming.time.value); } } ``` its not working I tried this also, not working ``` var re = /^(?:[01][0-9]|2[0-3]):[0-5][0-9]$/; ``` let me where I am going wrong?

Original source