RadTextbox to restrict special characters

7 posts, 1 answers
  1. usr9999
    usr9999 avatar
    41 posts
    Member since:
    Jun 2010

    Posted 19 Jul 2010 Link to this post

    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
  2. Answer
    Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 20 Jul 2010 Link to this post

    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.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. usr9999
    usr9999 avatar
    41 posts
    Member since:
    Jun 2010

    Posted 20 Jul 2010 Link to this post

    Thanks for the update. It works.
  5. Amol G
    Amol G avatar
    5 posts
    Member since:
    Mar 2011

    Posted 25 Apr 2012 Link to this post

    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.
  6. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 25 Apr 2012 Link to this post

    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.
  7. Amol G
    Amol G avatar
    5 posts
    Member since:
    Mar 2011

    Posted 25 Apr 2012 Link to this post

    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
  8. Vasil
    Admin
    Vasil avatar
    1547 posts

    Posted 30 Apr 2012 Link to this post

    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.
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017