如何停止在React Native中的密码输入字段中输入空格?

password可以是任何字符,包括空格.

我试过这个:validator.js

const extract = (str, pattern) => (str.match(pattern) || []).pop() || '';

export function removeWhiteSpace(str) {
  return extract(str, '/^\S*$/;');
}

login.js

passwordHandle(value){
  this.setState({
      password:removeWhiteSpace(value)
  })
  console.log(removeWhiteSpace(value))
}

render()

<View style={{paddingBottom:25}}>
  <TextField 
    label='Password'
    type='password' 
    value={password}
    error={errors.password}
    icon
    onChange={this.passwordHandle}/>
  <Image 
    source={require('../../../assets/img/lock.png')}
    style={styles.icon} />
</View>

但它不起作用.

推荐答案

这个简单的正则表达式应该使用.replace函数:

passwordHandle(value){
   this.setState({
       password: value.replace(/\s/g, '')
   })
}

React-native相关问答推荐

Redux toolkit通过按钮操作进行查询

react-native-fs 改变 toUrl 本身

打开屏幕后自动显示键盘

在 TouchableOpacity 上创建 Raised凸起或Shadow阴影效果

变量 window在react-native 中代表什么?

最近 react-native 版本中的Unable to resolve moduledebuggerWorker 错误

错误:看起来您在另一个中嵌套了一个NavigationContainer

如何将参数传递给graphql片段?

如何判断 React Native 中的 AsyncStorage 中是否存在密钥? getItem() 总是返回一个promise对象

FlatList contentContainerStyle -> justifyContent: 'center' 导致滚动问题

React Native Remote Debugger 在 Chrome 中显示缓存的包

如何从 React Native-App 中的 res/drawable-folder 加载图像?

React native Redux - 对象不是构造函数(判断'new ctor(props context)')

如果我有 AngularJS 基础知识,怎么学习Thinking in ReactJS呢?

带有 React Native 的 EventEmitter 和 Subscriber ES6 语法

在 React Native ScrollView 中检测滚动结束,对齐页面

React native - 以编程方式判断是否启用了远程 JS 调试

React Native - 为什么我需要 babel 或 webpack?

React Native 多行TextInput,文本居中

类型 androidx.appcompat.resources.R$dimen 被定义了多次