I am using react-native with react-hooks. I want to add some characters automatically like '-' or '(space)' in card number and '/' in due date. It should work like below.
If user enter numbers in card number input than '-' should add automatically.
1234 -> 1234- -> 1234-5678 -> 1234-5678-.
Also at due date, I want to add '/'.
02 -> 02/ -> 02/23
const CardAdd = () => {
const [cardNumber, setCardNumber] = useState("");
const [dueDate, setDueDate] = useState("");
onInputChange = (setState, value) => {
setState(value);
};
if (dueDate.length === 2) {
setDueDate(dueDate + "/");
}
return (
<View style={styles.container}>
<Input
name="Card Number"
keyboardType="number-pad"
style={styles.inputContainer}
placeholder="1234 5678 1234 5678"
value={cardNumber}
onChangeText={value => onInputChange(setCardNumber, value)}
/>
<Input
name="Due Date"
keyboardType="number-pad"
style={styles.inputContainer}
placeholder="MM/YY"
value={dueDate}
onChangeText={value => onInputChange(setDueDate, value)}
/>
</View>
);
};
If I do it like this, it does work. It adds '/' automatically when I type in 2 digits at due date. But I can't delete the after than. How can I solve this problem?