首页 > 解决方案 > 8位数字的正则表达式,之后是1个点,之后只有2位数字

问题描述

我需要一个正则表达式来接受八位数字,然后是一个点,之后只有两位数字用于金额字段。

目前我正在使用这么多代码......

valLowLTL(val) {
    const val1 = val.split('.');
    const amountLowLTL = this.addBillingForm.get('invoicingAmtLowLTL') as FormControl;
    const re = /,/gi;
    val1[0] = val1[0].replace(re, '');
    if (val1[0].length > 8) {
      val1[0] = val1[0].substring(0, 8);
    }
    amountLowLTL.setValue(val1[0]);
    if (val1.length > 1) {
      if (val1[1].length > 2) {
        val1[1] = val1[1].substring(0, 2);
      }
      const lowValLTL = val1[0].concat('.').concat(val1[1]);
      amountLowLTL.setValue(lowValLTL);
    }
}

我只需要一个简单的正则表达式来帮助我。

标签: javascriptregex

解决方案


您可以使用正则表达式: ^\d{8}[.]\d{2}$


推荐阅读