简体   繁体   中英

JavaScript regular expression to check time format

I would like to have a regular expression which clears an HTML field when the input is not correct. I created this one, but instead of clearing the field when it's incorrect, it clears it only when the input is correct.

onchange="this.value=this.value.replace(/^([01]?[0-9]|2[0-3]):[0-5][0-9]/,'')"

I've tried to 'reverse' the code, but it didn't succeed so far.

onchange="if (!this.value.match(/^([01]?[0-9]|2[0-3]):[0-5][0-9]/)) this.value = '';"
if(!/^([01]?[0-9]|2[0-3]):[0-5][0-9]/.test(this.value)) this.value="";
onchange="this.value=
/^([01]?[0-9]|2[0-3]):[0-5][0-9]/.test(this.value) ? this.value : ''"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM