【问题标题】:Validating input text field, do not accept first character as a number and special character验证输入文本字段,不接受第一个字符作为数字和特殊字符
【发布时间】:2021-09-05 17:26:20
【问题描述】:

在输入文本字段中允许有效大小写并防止无效大小写

输入字段必须接受以下内容:

有效案例
qw12@ qwe12
qweryt@123

并且不接受以下内容:

大小写无效
1a@
@qwe1
1@qwer

【问题讨论】:

  • 请提供足够的代码,以便其他人更好地理解或重现问题。

标签: javascript html jquery input-field


【解决方案1】:

你可以这样做,让你的 textInput 成为一个受控组件

const [textInputValue, setTextInputValue] = useState('')

  const onTextInputChange = (newText) => {
    //Here you can manipulate the value of the textinput as you want

    setTextInputValue(newText)
  }

  return (
    <View style={styles.container}>
      <TextInput style={styles.textInput} value={textInputValue} onChangeText={onTextInputChange} />
    </View>
  );

考虑到使用此解决方案,如果用户键入速度比 react-native 能够更新本机视图,您可能会遇到一些闪烁问题。

否则,您可以尝试在本地实现它,例如 here's TextInput 组件的自定义包装器示例,它允许将正则表达式传递给白名单允许的字符

【讨论】:

    猜你喜欢
    • 2013-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-13
    • 2017-10-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多