RadInput for ASP.NET AJAX

RadControls for ASP.NET AJAX

The OnKeyPress client-side event handler is called when the user presses a key to enter a value. It does not occur when the user presses navigation keys, the delete key, and so on.

Note

The OnKeyPress event is supported by all RadInput controls.

Two parameters are passed to the event handler:

  • sender is the input control.

  • eventArgs has the following methods:

    • set_cancel() lets you prevent the key press from entering a new character into the input control.

    • get_keyCode() returns the key code for the key that was pressed.

    • get_keyCharacter() returns the character that is about to be entered into the input control in response to the key press.

    • get_domEvent() returns the DOM event object for the key press.

The following example uses the OnKeyPress event to limit input to the letters 'a' to 'z' and 'A' to 'Z':

CopyASPX
<telerik:RadTextBox ID="RadTextBox1" runat="server">
    <ClientEvents OnKeyPress="AlphabetOnly" />
</telerik:RadTextBox>
CopyJavaScript
<script type="text/javascript">
    function AlphabetOnly(sender, eventArgs)
    {
        var c = eventArgs.get_keyCode();
        if ((c < 65) || (c > 90 && c < 97) || (c > 122))
            eventArgs.set_cancel(true);
    }
</script>

See Also

Other Resources