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

Block Special Characters in RadTextBox

3 Answers 351 Views
Input
This is a migrated thread and some comments may be shown as answers.
Shajan
Top achievements
Rank 1
Shajan asked on 17 Dec 2012, 11:28 AM
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

3 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 17 Dec 2012, 12:38 PM
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.
0
Shajan
Top achievements
Rank 1
answered on 17 Dec 2012, 01:35 PM
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
0
Shinu
Top achievements
Rank 2
answered on 18 Dec 2012, 05:39 AM
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.
SMc
Top achievements
Rank 2
Iron
Veteran
Iron
commented on 16 Feb 2023, 10:51 PM

I wanted to say thank you for the js code. I got it to work! I only wanted to restrict "<" and ">" as the users were getting "Dangerous Code Injection" errors!


function OnKeyPress(sender, eventArgs) {
			var char = eventArgs.get_keyCharacter();
			if (char == '<' || char == '>') {
				eventArgs.set_cancel(true);
			}
		}

Was all I needed.

Thanks again!

Rumen
Telerik team
commented on 17 Feb 2023, 08:17 AM

Thank you for sharing your solution for the < and > symbols, Sean!

Fore reference I am also adding these two resources on the topic here:

Tags
Input
Asked by
Shajan
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Shajan
Top achievements
Rank 1
Share this question
or