You can also do this and you don't have to use if or any other statement...
First of all download this package to your root folder, this is a inbuild package of npm to download type this command in your cmd prompt....
npm i react-native-validator-form
Then import it to your project.
import { Form, TextValidator } from 'react-native-validator-form';
Then create a class and extend it with React.Component and copy-paste the following code...
state = {
phonenumber: ''
}
handleSubmit = () => {
this.refs.form.submit();
}
render() {
const {phonenumber} = this.state;
return (
<Form
ref="form"
onSubmit={this.handleSubmit}
>
<TextValidator
name="phonenumber"
validators={['required', 'isNumber','maxNumber:11']}
errorMessages={['Phonenumber is required', 'Phonenumber invalid' , 'Not a valid number ']}
placeholder="Phonenumber"
value={phonenumber}
onChangeText={(phonenumber) => this.setState({phonenumber})}
/>
<Button
title="Submit"
onPress={this.handleSubmit}
/>
</Form>
);
}