Texinput没有空格?

时间:2018-11-15 03:06:25

标签: reactjs react-native textinput

我需要在用户填写文本字段时输入空白时提醒。在执行此操作时,我不希望键盘消失。我不知道如何做到这一点吗?

1 个答案:

答案 0 :(得分:1)

您可以为此使用regex

import React, { Component } from "react";
import {  View, TextInput } from "react-native";


class App extends Component {

  state = {
     text : ''
  }

  hasWhiteSpace(s) {
    let regSpace= new RegExp(/\s/);
    // Check for white space
    if (regSpace.test(s)) {
      //your logic
      alert("Please Check Your Fields For Spaces");
      return false;
    } else {
       this.setState({ text:s })
    }
    return true;
  }

  render() {
    return (
      <View>
         <TextInput
            style={{ height: 40, borderColor: "gray", borderWidth: 1 }}
            onChangeText={text => {
            this.hasWhiteSpace(text);
          }}
          value={this.state.text}
        />
      </View>
    );
  }
}

export default App;

工作示例

https://codesandbox.io/embed/04jl3800vw