Created
January 15, 2017 04:28
-
-
Save AlexisLeon/80b5641eb30b43bc598288e41052ac39 to your computer and use it in GitHub Desktop.
TextInput accepts only numbers - React Native
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { Component } from 'react'; | |
import { TextInput } from 'react-native'; | |
class Input extends Component { | |
constructor(props) { | |
super(props); | |
this.state = { | |
text: '' | |
}; | |
} | |
handleInputChange = (text) => { | |
if (/^\d+$/.test(text)) { | |
this.setState({ | |
text: text | |
}); | |
} | |
} | |
render () { | |
return ( | |
<TextInput | |
keyboardType='numeric' | |
onChangeText={this.handleInputChange} | |
value={this.state.text} | |
/> | |
) | |
} | |
} |
Thanks
what about
-5
.2
0.5
-0.5
It ain't working!!!
It's working! Thx!
Nice snippet, I tried the code and discovered a bug though. You can't set the text back to blank. Here is how i changed the code
if (/^\d+$/.test(amount) || amount === '') { this.setState({ amount }) }
this works great!!!
How can we do the same for Input Field
in React Native. Please find my sample code :
CODE:
ONCHANGETEXT CODE`::
Nice snippet, I tried the code and discovered a bug though. You can't set the text back to blank. Here is how i changed the code
if (/^\d+$/.test(amount) || amount === '') { this.setState({ amount }) }
Great, this works excellent
Thanks
This was very helpful
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thanks!
and @gate3 too for updated code