Block Special Characters in RadTextBox

4 posts, 0 answers
  1. Shajan
    Shajan avatar
    4 posts
    Member since:
    Jun 2012

    Posted 17 Dec 2012 Link to this post

    Hi all,

    I am trying to filter a rad textbox in client side. I need to restrict special characters (eg. $, ^, # [,]) in it. How can I implement this?

    Please provide me a solution for this.

    Thanks and Regards
    Shajan
  2. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 17 Dec 2012 Link to this post

    Hi Shajan,

    Try the following code snippet.

    ASPX:
    <telerik:RadTextBox ID="RadTextBox1" runat="server" ClientEvents-OnKeyPress="OnKeyPress">
    </telerik:RadTextBox>

    JS:
    <script type="text/javascript">
        function OnKeyPress(sender, eventArgs) {
            var char = eventArgs.get_keyCharacter();
            if (char == '$' || char == '#' || char == '^' || char == '[' || char == ']') {
                eventArgs.set_cancel(true);
            }
        }
    </script>

    Regards,
    Shinu.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Shajan
    Shajan avatar
    4 posts
    Member since:
    Jun 2012

    Posted 17 Dec 2012 Link to this post

    Dear Shinu,

    I implemented this code, with some more special characters(&;`'\|*?~<>^()[]{}$&quot;). But in Fire Fox "The Right Arrow Key" not working.


    Please provide me a solution for this.

    Thanks and Regards
    Shajan
  5. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 17 Dec 2012 Link to this post

    Hi Shajan,

    The behavior that you found is because the keycode of ' conflict with right arrow key. With respect to this forum thread you can use the get_domEvent() to get the original DOM event.

    JS:
    <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>

    Regards,
    Shinu.
Back to Top