【问题标题】:Textinput minimum length React native文本输入最小长度反应原生
【发布时间】:2019-02-19 08:16:30
【问题描述】:

有没有办法将文本输入限制在最小长度和最大长度之间。假设我想将 textinput 长度限制在 5 到 15 之间,我该怎么做?

【问题讨论】:

  • 什么时候进行检查?在文本更改期间?在提交期间?或在组件安装期间?对于最大值,TextInput 组件有一个 maxLength 属性... (facebook.github.io/react-native/docs/textinput#maxlength)
  • 点击按钮会有提示,用户可以在其中输入字母数字序列号,该代码的长度应为 5-15。在那个提示中有一个确定按钮,在按钮单击时我需要验证输入的输入是否是字母数字并且长度是否合适。
  • 那么是什么阻止了您实施呢?您可以获取文本并检查长度是否在该范围内。或者你可以实现 maxLength 属性并且只检查最小长度
  • 如何只检查最小长度?

标签: react-native react-native-android react-native-ios react-native-textinput


【解决方案1】:

考虑在您的组件中添加以下代码:

<TextInput onTextChange={this.onTextChange} maxLength={15} ... />
<Button onPress={this.onPress} ... >Submit</Button>

onTextChange = text => {
   this.setState({text : text});
}

onPress = () => {
   const {text} = this.state;

   if(text.length < 5) {
      console.log('Your text is less than what is required.');
   }
}

【讨论】:

    【解决方案2】:

    您可以使用redux-form 进行操作,按照以下步骤操作

    we.js

    module.exports = {
    
        reqMsg: 'Required',
    
        maxLength: max => value => value && value.length > max ? `Must be ${max} characters or less` : undefined,
    
        minValue: min => value => value && value.length < min ? `Must be at least ${min} characters` : undefined,
      };
    

    validations.js

    import { reqMsg, maxLength, minValue } from './we';
    module.exports = {
        //Validation
        required: value => value ? undefined : reqMsg,
    
        maxLength15: maxLength(15),
    
        minValue5: minValue(5)
    };
    

    UserCreateForm.js

    import React, { Component } from 'react';
    import { Field, reduxForm } from 'redux-form';
    import { Item, Input, CheckBox, ListItem, Spinner, Icon } from 'native-base';
    import { required, minValue5, maxLength15} from './validations';
    
    
    const renderField = ({ secureTextEntry, iconType, iconName, keyboardType, placeholder, meta: { touched, error, warning }, input: { onChange, ...restInput } }) => {
        return (
            <View>
                <Item error={touched && !!error} rounded>
                    <Icon type={iconType} name={iconName} />
                    <Input secureTpickerStyleextEntry={JSON.parse(secureTextEntry)} keyboardType={keyboardType}
                        onChangeText={onChange} {...restInput} placeholder={placeholder} autoCapitalize='none'>
                    </Input>
                    {touched && !!error && <Icon name='close-circle' />}
                </Item>
                    {touched && (!!error && <Text>{error}</Text>)}
            </View>
        );
    };
    
    class UserComponent extends Component {
    
        render() {
    
            return (
    
                    <Field name="Name" iconType="SimpleLineIcons" iconName="user" secureTextEntry="false" keyboardType="default" placeholder="FirstName LastName NikeName" component={renderField}
                        validate={[required, minValue5, maxLength15]}
                    />
            );
        }
    }
    
    const UserCreateForm = reduxForm({
        form: USER_CREATE_FORM // a unique identifier for this form
    })(UserComponent);
    
    export default UserCreateForm;
    

    【讨论】:

      【解决方案3】:

      之前的评论也不错,但是时间和空间复杂度比较高。为此克服使用此代码。

      <TextInput onTextChange={this.onTextChange} maxLength={15} ... />
      
      onTextChange=()=>{
      
       if (value ==^[a-zA-Z0-9]{5,15}$) {
              alert( "Input is valid\n");
          } else {
              alert( "Input is invalid\n");
          }
      }
      

      这个代码帮我用这个代码,你也可以重置限制长度,改变值 这里 5 :- 最低 15:- 最大值。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-05-04
        • 1970-01-01
        • 1970-01-01
        • 2022-01-21
        • 1970-01-01
        • 2020-12-19
        • 1970-01-01
        相关资源
        最近更新 更多