React native Textinput最小长度

React native Textinput最小长度,react-native,react-native-android,react-native-ios,react-native-textinput,React Native,React Native Android,React Native Ios,React Native Textinput,有没有办法将文本输入限制在最小长度和最大长度之间。假设我想将textinput长度限制在5到15之间,我该怎么做?考虑在组件中添加以下代码: <TextInput onTextChange={this.onTextChange} maxLength={15} ... /> <Button onPress={this.onPress} ... >Submit</Button> onTextChange = text => { this.setSta

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

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

<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.');
   }
}

提交
onTextChange=text=>{
this.setState({text:text});
}
onPress=()=>{
const{text}=this.state;
如果(文本长度<5){
log('您的文本少于所需内容');
}
}
您可以使用以下步骤进行操作

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,
  };
import { reqMsg, maxLength, minValue } from './we';
module.exports = {
    //Validation
    required: value => value ? undefined : reqMsg,

    maxLength15: maxLength(15),

    minValue5: minValue(5)
};
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;
UserCreateForm.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,
  };
import { reqMsg, maxLength, minValue } from './we';
module.exports = {
    //Validation
    required: value => value ? undefined : reqMsg,

    maxLength15: maxLength(15),

    minValue5: minValue(5)
};
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;
import React,{Component}来自'React';
从“redux form”导入{Field,reduxForm};
从“本机基”导入{Item,Input,CheckBox,ListItem,微调器,Icon};
从“/validations”导入{required,minValue5,maxLength15};
const renderField=({secureTextEntry,iconType,iconName,keyboardType,占位符,元:{touched,error,warning},input:{onChange,…restInput})=>{
返回(
{触摸&&!!错误&&}
{触摸&(!!错误&&{error})}
);
};
类UserComponent扩展组件{
render(){
返回(
);
}
}
const UserCreateForm=reduxForm({
表单:用户\u创建\u表单//此表单的唯一标识符
})(用户组件);
导出默认UserCreateForm;

前面的评论也不错,但它有更多的时间和空间复杂性。为此,请使用此代码

<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");
    }
}

onTextChange=()=>{
如果(值==^[a-zA-Z0-9]{5,15}$){
警报(“输入有效\n”);
}否则{
警报(“输入无效\n”);
}
}
此代码帮助我使用此代码,还可以重置长度限制,更改值 这里5:-最小值
15:-最大值。

何时进行检查?在文本更改期间?提交期间?还是在组件安装期间?对于max,TextInput组件有一个
maxLength
prop。。。()点击按钮时会出现提示,用户可以输入字母数字序列码,该码的长度应为5-15。在这个提示中有一个OK按钮,在点击按钮时,我需要验证输入的是字母数字还是长度。那么是什么阻止了你实现它呢?您可以获取文本并检查长度是否在该范围内。或者您可以实现
maxLength
prop并只检查最小长度如何只检查最小长度?