如何在textInput React Native中停止输入空格?

时间:2018-02-14 11:12:13

标签: javascript reactjs react-native

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

#config for session 1 SenderCompID=Sender TargetCompID=Target SessionQualifier=FirstSession ...... #config for session 2 SenderCompID=Sender TargetCompID=Target SessionQualifier=SecondSession ...... 可能是任何字符,包括空格。

我试过这个: validator.js

QuickFix.Session.SendToTarget(msg, sessionID);
//where sessionID = "FIX.4.4:Sender->Target" which is identical for both sessions

login.js

password

渲染()

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

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

但它不起作用。 它只执行'/ ^ \ S * $ /;'来自passwordHandle(value){ this.setState({ password:removeWhiteSpace(value) }) console.log(removeWhiteSpace(value)) }

3 个答案:

答案 0 :(得分:10)

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

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

答案 1 :(得分:1)

如果您只关心在字符串的末尾(或前面)输入空格,您也可以使用原型 String 对象中的 trim()。

passwordHandle(value){
   this.setState({
       password: value.trim()
   })
}

关于trim() 的更多信息:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/Trim

答案 2 :(得分:0)

您可以像这样简单地验证 onChangeText 函数中的空格

onChangeText={(text) => {
 if (text.includes(' ')) {
   setText(text.trim()); 
  } else {
   setText(text);
  }
 }
}