This is a migrated thread and some comments may be shown as answers.

RadTextbox to restrict special characters

6 Answers 1186 Views
Input
This is a migrated thread and some comments may be shown as answers.
usr9999
Top achievements
Rank 1
usr9999 asked on 20 Jul 2010, 01:39 AM
Hi

I have a requirement to restrict a user from entering special character in the Radtextbox. I have tried both RegularExpressionValidator and CustomValidator with ClientValidationFunction, but both are capturing after user completes the entry. Just curious to know if we have any control just like AjaxTextbox extender so user will not be able to enter invalid characters instead of validating after the data entry.

Thanks

6 Answers, 1 is accepted

Sort by
0
Accepted
Shinu
Top achievements
Rank 2
answered on 20 Jul 2010, 11:02 AM
Hello,

It is doable by attaching the 'OnKeyPress' event to RadTextBox and then cancel the event if pressed invalid character.


Go through the link below:
OnKeyPress


-Shinu.
0
usr9999
Top achievements
Rank 1
answered on 20 Jul 2010, 03:50 PM
Thanks for the update. It works.
0
Amol G
Top achievements
Rank 2
answered on 25 Apr 2012, 11:51 AM
Hi,

I have check this code it working fine for IE browser but for Mozilla fire fox  it restrict delete,left right,up and down key also.
0
Shinu
Top achievements
Rank 2
answered on 25 Apr 2012, 12:28 PM
Hi Amol,

I suppose that you want to allow delete, left arrow, right arrow, down arrow and up arrow while limiting input to the letters 'a' to 'z' and 'A' to 'Z'.
Here is the sample code that i tried which worked as expected.

JS:
<script type="text/javascript">
  function OnKeyPress(sender, args)
    {
        var c = args.get_keyCode();
        if ((c < 37) || (c > 40 && c < 45) || (c > 47 && c < 65) || (c > 90 && c < 97) || (c > 122))
            args.set_cancel(true);
    }
</script>

Thanks,
Shinu.
0
Amol G
Top achievements
Rank 2
answered on 25 Apr 2012, 12:40 PM
Hi
Thanks for quick reply i try updated code but in case of fire box now it allow following charcters
'%' , '&', ' ' ', ','
and i found that keycode for above keys conflict with other keys(delete,up,down,left and right).

please see here  http://javascript.info/tutorial/keyboard-events

Thanks
AMOL G
0
Vasil
Telerik team
answered on 30 Apr 2012, 10:59 AM
Hello,

Use the get_domEvent() to get the original DOM event. It will have the information that you need about exact characters that was hold during the key-press:
<script type="text/javascript">
  function OnKeyPress(sender, args)
  {
    var domEvent = args.get_domEvent();
 
    alert("control is held: " + domEvent.ctrlKey);
    alert("alt is held: " + domEvent.altKey);
  }
</script>
<telerik:RadTextBox runat="server" ID="RadTextBox123">
  <ClientEvents OnKeyPress="OnKeyPress" />
</telerik:RadTextBox>

See this for more information: https://developer.mozilla.org/en/DOM/KeyboardEvent

All the best,
Vasil
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
Tags
Input
Asked by
usr9999
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
usr9999
Top achievements
Rank 1
Amol G
Top achievements
Rank 2
Vasil
Telerik team
Share this question
or