javascript - Automatically takes a space after 4 letters in text input field in react-native - Stack Overflow

If i entered text in text-input field then automatically it takes a space after 4 digits.Here is my cod

If i entered text in text-input field then automatically it takes a space after 4 digits.

Here is my code for text-input field:

handleCardNumber = (text) => {
    this.setState({ cardNumber: text })
}

<TextInput
    ref="first"
    underlineColorAndroid="transparent"
    autoCapitalize="none"
    style={styles.cardNumberTextInput}
    placeholder="1234 1234 1234 1234"
    placeholderTextColor="grey"
    returnKeyType='next'
    keyboardType = {'numeric'}
    onChangeText={this.handleCardNumber}
    onSubmitEditing={(event)=>{
    this.refs.second.focus();
}}

Here is my screenshot:

If i entered text in text-input field then automatically it takes a space after 4 digits.

Here is my code for text-input field:

handleCardNumber = (text) => {
    this.setState({ cardNumber: text })
}

<TextInput
    ref="first"
    underlineColorAndroid="transparent"
    autoCapitalize="none"
    style={styles.cardNumberTextInput}
    placeholder="1234 1234 1234 1234"
    placeholderTextColor="grey"
    returnKeyType='next'
    keyboardType = {'numeric'}
    onChangeText={this.handleCardNumber}
    onSubmitEditing={(event)=>{
    this.refs.second.focus();
}}

Here is my screenshot:

Share Improve this question edited Sep 18, 2017 at 11:36 JustBaron 2,3477 gold badges25 silver badges37 bronze badges asked Sep 18, 2017 at 10:20 HarikaHarika 1,1313 gold badges14 silver badges25 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 4

Replace your code with below:

handleCardNumber = (text) => {
  let formattedText = text.split(' ').join('');
  if (formattedText.length > 0) {
    formattedText = formattedText.match(new RegExp('.{1,4}', 'g')).join(' ');
  }
  this.setState({ cardNumber: formattedText });
  return formattedText;
}

Add this line in your input code to show changes state value:

value={this.state.cardNumber}

JS Demo: (enter more than 4 digits)

handleCardNumber = (text) => {
      let formattedText = text.split(' ').join('');
      if (formattedText.length > 0) {
        formattedText = formattedText.match(new RegExp('.{1,4}', 'g')).join(' ');
      }
      //this.setState({ cardNumber: text });
      console.log(formattedText)
      return formattedText;
    }
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="number" onchange="handleCardNumber(this.value)" >

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745306422a4621736.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信