蔡香满屋
站在牛顿头上吃苹果

匹配不能大于100的正则是:

pattern: /^100$|^([0-9]{0,2})(\.\d{0,3})?$/,
当输入数字大于100时,则会提示如图:

鼠标移除输入框焦点后则会默认变成100,红色提示消失如图:

代码实现如下:

 <FormItem {...formItemLayout} label="成绩系数:">
                        {getFieldDecorator('scoreCoefficientValue', {
                            rules: [
                                {
                                    required: true,
                                    message: '必填'
                                },
                                {
                                    pattern: /^100$|^([0-9]{0,2})(\.\d{0,3})?$/,
                                    message: '不能大于100,小数点后最多两位'
                                }
                            ]
                        })(
                            <InputNumber
                                style={{width: '100%'}}
                                placeholder="请输入小于100的数"
                                min={0}
                                max={100}
                                formatter={this.limitDecimals}
                                parser={this.limitDecimals}/>
                        )}
                    </FormItem>

  计算输入只能输入4位有效数字,最多只能输入两位小数点。

代码实现方法如下:

// 处理输入框只能输入两位小数的方法
    limitDecimals = (value) => {
        const reg = /^(\-)*(\d+)\.(\d\d).*$/;
        if (typeof value === 'string') {
            return !isNaN(Number(value)) ? value.replace(reg, '$1$2.$3') : ''
        } else if (typeof value === 'number') {
            return !isNaN(value) ? String(value).replace(reg, '$1$2.$3') : ''
        } else {
            return ''
        }
    };

  匹配不能大于100000的正则:

/^((\d{1,5})|(\d{1,4}\.\d{1})|(\d{1,3}\.\d{1,2}))$/

 


posted on 2019-02-12 11:35  蔡香满屋  阅读(3151)  评论(0)    收藏  举报