I have a phone number input in my React App. I want to replace non numeric characters that the user types to spaces. I have written the following regex function to do that:
utils.js
/**
* Phone Number - Only allow numbers
*/
export const phoneReplacer = el => {
let elem = new d(el)
let regex = /[^0-9]/i
elem.on('keyup', e => {
let value = e.currentTarget.value
elem.setValue(value.replace(regex, ''))
})
}
How to hook this up to my input component which is below:
import React from 'react'
import PropTypes from 'prop-types'
import TextInput from '../others/input/text'
import phoneReplacer from '../../../utils'
const PhoneInput = ({ value, change }) => (
<div className="edit_phone_div">
<TextInput
type="text"
placeholder="Phone Number"
value={value}
valueChange={e => change('phone', e)}
/>
</div>
)