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} | |
/> | |
) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Great, this works excellent