javascript - react native : mobile number validation accept numeric value only

标签 javascript reactjs react-native react-native-ios

如何在 React Native 中验证手机号码是否接受唯一的数值。

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

我使用了 keyboardType="numeric" 但它也接受特殊字符,所以我想接受唯一的数值。

最佳答案

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

 <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)}  
     }
 />

关于javascript - react native : mobile number validation accept numeric value only,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50441460/

相关文章:

javascript - 如何在React Native中获取选定的元素?

javascript - Ember.js - 使用输入值动态构建链接

javascript - 如何使搜索中的选项始终可见

Javascript 随条件减少

javascript - 如何修复 angular.module 无法与依赖模块一起使用的问题?

javascript - 如何在 React Redux 中获取状态的先前值

javascript - 在 React 中使用 setState 进行多个获取请求

javascript - 构建运行时挂起 'react-native run-ios'

javascript - 如何在 React.js 中使用 jQuery 依赖节点模块?

react-native - @react-native-community/netinfo : NativeModule. RNCNetInfo 安装最新版本后为空