I encountered an issue with a textbox I created to validate U.S. cellphone numbers using a JavaScript function that only allows numbers. The problem arises when I try to clear the textbox after entering an incorrect number, as the JS function prevents me from doing so.
Below is my code snippet:
function NumbersOnly(sender, args) {
var text = sender.get_value() + args.get_keyCharacter();
if (!text.match('^[0-9]+$') && text != '\b' && text != '(' && text != ')' && text != '-') {
args.set_cancel(true);
}
}
Cell:
</td>
<td style="text-align: left">
<telerik:RadTextBox ID="txtPhone" runat="server" MaxLength="25" Width="200px"><ClientEvents OnKeyPress="NumbersOnly"></ClientEvents></telerik:RadTextBox>
<asp:RegularExpressionValidator runat="server" ID="revPhone"
ControlToValidate="txtPhone"
Display="None"
ValidationExpression="^(\([0-9]{3}\) |[0-9]{3}-)[0-9]{3}-[0-9]{4}$"
ValidationGroup="validateEditUser"
EnableClientScript="false"
ErrorMessage="- Please enter a valid phone number:(ex.) <b>(xxx-xxx-xxxx)</b></br>" />
</tr>
Thanks