Replace the code 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 to your input code to show the change status 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(' ');
}
console.log(formattedText)
return formattedText;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="number" onchange="handleCardNumber(this.value)" >
Run code source
share