jQuery验证,允许两位后的点和冒号

时间:2018-10-10 10:12:59

标签: javascript regex

$('#total_duration').keyup(function(event) {
  this.value = this.value
  .replace(/[^\d:]/g, '')             // numbers and decimals only
  .replace(/(^[\d]{2})[\d]/g, '$1')   // not more than 2 digits at the beginning
  .replace(/(\::*)\s:/g, '$1')         // decimal can't exist more than once
  .replace(/(\:[\d]{2})./g, '$1');    // not more than 4 digits after decimal         
});

这允许冒号在两位数之后,例如11:12,但我也希望在模式中也使用点(。),例如11.12

小提琴链接:https://jsfiddle.net/8dLb06je/

0 个答案:

没有答案