0

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> 

2 Answers 2

1

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,'')});
}
Sign up to request clarification or add additional context in comments.

Comments

0

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})
}

3 Comments

thnks but if there is any possible to disable it?
it won't display in a screen at all have to disable.
don't have idea about that :), but i think that is not possible.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.