Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Native: mobile number validation accept numeric value only

How validate mobile number in react native accept the only numeric value.

 <TextInput
      ref='mobileNo'
      keyboardType="numeric"
      style={[styles.textInput, { width: '100%' }]}
      placeholder='Enter mobile number'
      onChangeText={(value) => this.handleChange('mobileNo', value)}   />

I have used keyboardType="numeric" but its accept special character also so I want to accept the only numeric value.

like image 371
Asif vora Avatar asked May 21 '18 03:05

Asif vora


1 Answers

Use this validation.

mobilevalidate(text) {
    const reg = /^[0]?[789]\d{9}$/;
    if (reg.test(text) === false) {
      this.setState({
        mobilevalidate: false,
        telephone: text,
      });
      return false;
    } else {
      this.setState({
        mobilevalidate: true,
        telephone: text,
        message: '',
      });
      return true;
    }
  }
like image 182
Vishal Vaghasiya Avatar answered Oct 28 '22 08:10

Vishal Vaghasiya