最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

regex - JavaScript regular expression to check time format - Stack Overflow

programmeradmin1浏览0评论

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.

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.

Share Improve this question edited Jun 17, 2012 at 6:02 Darshan Rivka Whittle 34.1k7 gold badges96 silver badges113 bronze badges asked Jun 16, 2012 at 15:51 Maurice van der LMaurice van der L 131 silver badge3 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 5
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 : ''"
发布评论

评论列表(0)

  1. 暂无评论