On a form I have an <input type = "text"> field for a 10 digit-only telephone number. I need the field as and when the user inputs (by typing or pasting) to allow 10 numbers only. When inserting a letter, symbol or space, these invalid characters should be ignored/stripped (on q01234 56789p, only 0123456789 should remain).
If the telephone number starts with 27 it must convert 27 to a 0 but keep the last digit. Pasting data starting with 27 it must keep everything after the 27 as telephone numbers beginning with 27 have 11 digits, exceeding the 10 digit limit (27123456789 must become 0123456789, not 012345678).
How can this be done as the user pastes into the field instead of waiting to loose focus first? All conditions must validate and correct input as the user attempts to go on to the next input field. This form is for typing or pasting data from logs that have either omitted the leading zero or include the +27 international code, in Firefox 66x Quantum. I don’t mind changing the input field as long as it remains a text box and not a text field or number input type.
<form>
<label>Phone Number</label><br>
<input id="PhoneNumber" type="text" minlength="10" maxlength="10" pattern="[0][0-9]" required>
</form>
teltype on an input field, as Crypopat suggested. You can still specify your own regex pattern and error state