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 badges3 Answers
Reset to default 5onchange="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 : ''"