不能输入:负号(-)零开头(0)
正则算法
formatter = (value) => (`${value.toString().replace(/^(0+)|[^\d]+/g, '')}`)
parser = (value) => (`${value.toString().replace(/^(0+)|[^\d]+/g, '')}`)
使用
formatter={this.formatter}
parser={this.parser}
例:
'use strict'
import React from 'react'
import {
InputNumber
} from 'antd'
import styles from '../../../../../../assets/Products.scss'
/**
* edit product SupplierReceiveGoodsDays (供应商到货天数)
*/
export default class ProductEditSupplierReceiveGoodsDays extends React.Component {
//不能为负数
formatter = (value) => (`${value.toString().replace(/^(0+)|[^\d]+/g, '')}`)
parser = (value) => (`${value.toString().replace(/^(0+)|[^\d]+/g, '')}`)
variationsChangeChildren = (value) => {
this.props.variationsChangeChildren(value, this.props.record, 'GoodsDays')
}
render () {
const { max, value, disabled } = this.props
return (
<div className={styles['input-group-content']}>
<span className={styles['input-group-addon']}>天</span>
<InputNumber
value={value}
min={0}
max={max}
disabled={disabled}
className={styles['input-number']}
style={{ width: '140px' }}
formatter={this.formatter}
parser={this.parser}
onChange={this.variationsChangeChildren}
/>
</div>
)
}
}