I have used this script recently. I would like to thank the author, but don't remember where on the internet I found it.
It removes letters from the phone number. The script is triggered on the OnChange of the field.
Your requirement is a bit different, but doable with regex, ask bing chat 😉
function validatePhoneNumber(context) {
var phoneNumberField = context.getEventSource(); // Get the field that triggered the onChange event
var phoneNumberValue = phoneNumberField.getValue();
var disallowedPattern = /[a-zA-Z]/g; // Regular expression to match all alphabet characters
if (phoneNumberValue && disallowedPattern.test(phoneNumberValue)) {
alert("Please enter a valid phone number without alphabet characters.");
// Replace alphabet characters with an empty string to remove them
phoneNumberValue = phoneNumberValue.replace(disallowedPattern, "");
phoneNumberField.setValue(phoneNumberValue); // Update the field value without alphabet characters
}
}