Reputation: 375
Need help, i was just placed material-ui
textfield in react native and I have set:
keyboardType: {numeric}
Its works fine but when I entered 30,000 it's showing NaN
I want to restrict comma(,) and dot(.) in keyboard. When I type amount it will calculate and show result in text.
<TextField
onChange={(event)=>this.handle(event.nativeEvent.text)}
label='Amount'
value={this.state.amount}
keyboardType = 'numeric'
enablesReturnKeyAutomatically={true}>
</TextField>
Upvotes: 0
Views: 3047
Reputation: 902
The displayed value will always be this.state.amount
. So if you change the value to discard everything but numerical digits, there will only be digits on the screen.
One extra thing you could do is change the keyboardType to 'number-pad', but I don't know how cross-device that is.
Use the following...
<TextField
onChange={(event)=>this.handleChange(event.nativeEvent.text)}
label='Amount'
value={this.state.amount}
keyboardType = 'number-pad'
enablesReturnKeyAutomatically={true}>
</TextField>
handleChange(event) {
this.setState({amount: event.target.value.replace(/[^0-9]/g,'')});
}
Upvotes: 1
Reputation: 104529
Try this:
Since you are using the controlled component, means storing the field value in state variable, So to restrict the user from entering input ,
and .
, put the check inside onChange
method, if entered value is integer then only update the state value otherwise not.
Like this:
<TextField
onChange={event => this.handle(event.nativeEvent.text)}
label='Amount'
value={this.state.amount}
keyboardType = 'numeric'
enablesReturnKeyAutomatically={true}>
</TextField>
handle(value){
if(/^\d+$/.test(value))
this.setState({amount: value})
}
Upvotes: 0