1

For example the user can enter: +444123454656656 OR 123454656656 - only numbers or a + in front of the number.

<TextFieldFormsy
    className="myclasse"
    variant="outlined"
    label={t("phone_number")}
    id="phone_number"
    name="phone_number"
    value={phone_number}
    onChange={handleChange}
/>
Alexandre
  • 11
  • 5

1 Answers1

0

You can simply filter the input in onChange to remove everyting you do not want:

const isValidChar = (char, index) => {
    if(index === 0 && char === "+") {
        return true;
    } 
    return !isNaN(char);
}

const filteredInput = input.split("").filter((char, index) => isValidChar(char, index)).join("")

here is a fiddle for the filter.

Domino987
  • 8,475
  • 2
  • 15
  • 38