首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Textinput最小长度反应原生

问Textinput最小长度反应原生
EN

Stack Overflow用户
提问于 2019-02-19 16:16:30
回答 3查看 16.3K关注 0票数 2

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

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2019-02-19 16:31:49

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

代码语言:javascript
复制
<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.');
   }
}
票数 3
EN

Stack Overflow用户

发布于 2019-02-19 16:51:43

您可以使用redux-form完成此操作,请执行以下步骤

we.js

代码语言:javascript
复制
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

代码语言:javascript
复制
import { reqMsg, maxLength, minValue } from './we';
module.exports = {
    //Validation
    required: value => value ? undefined : reqMsg,

    maxLength15: maxLength(15),

    minValue5: minValue(5)
};

UserCreateForm.js

代码语言:javascript
复制
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;
票数 0
EN

Stack Overflow用户

发布于 2019-02-19 16:56:01

之前的评论也很好,但它有更多的时间和空间复杂性。要克服这一点,请使用以下代码。

代码语言:javascript
复制
<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:-最大值。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54761560

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档