React Native:手机号码验证仅接受数字值

Asi*_*ora 5 javascript reactjs react-native react-native-android react-native-ios

如何在本机中验证手机号码接受唯一的数值。

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

我已经使用过,keyboardType="numeric"但是它也接受特殊字符,因此我想接受唯一的数值。

Vis*_*iya 7

使用此验证。

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;
    }
  }
Run Code Online (Sandbox Code Playgroud)


小智 6

尝试使用keyboardType='phone-pad'.


Nir*_*inh 5

您需要检查输入字符串是否为数字。检查以下代码:

 <TextInput
   ref='mobileNo'
   keyboardType="numeric"
   style={[styles.textInput, { width: '100%' }]}
   placeholder='Enter mobile number'
   onChangeText={(value) => 
   let num = value.replace(".", '');
     if(isNaN(num)){
         // Its not a number
     }else{
        this.handleChange('mobileNo', num)}  
     }
 />
Run Code Online (Sandbox Code Playgroud)