don't allow user to write special characters in RadTextBox

5 posts, 0 answers
  1. Mohammed
    Mohammed avatar
    87 posts
    Member since:
    Aug 2011

    Posted 14 Aug 2012 Link to this post

    Hi,
    how can I disable the user to write special characters in RadTextBox ? 
    I'm also using the 
    EmptyMessage property.

    I want something like ajax toolkit filter extender 
  2. Mohammed
    Mohammed avatar
    87 posts
    Member since:
    Aug 2011

    Posted 14 Aug 2012 Link to this post

    Hi my friends,
    i found a solution for my case and would like to share it with you : 

    <telerik:RadTextBox ID="rtxtSerialNo" runat="server" MaxLength="40" Width="198px"
    SelectionOnFocus="SelectAll" >
             <ClientEvents OnKeyPress="OnKeyPressSerialText"/>
    </telerik:RadTextBox>
    
    
    
    function OnKeyPressSerialText(sender, eventArgs) {
                var char = eventArgs.get_keyCharacter();
    	    //will allow just letters, numbers and "-" letter 
                var exp = /[^a-zA-Z0-9-]/g;             if (exp.test(char)) {                 eventArgs.set_cancel(true);             }         }
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Mayur
    Mayur avatar
    4 posts
    Member since:
    Jul 2012

    Posted 03 Sep 2012 Link to this post

    Hi Mohammed,

    I used your solution it works. But in firefox browser when i try to edit the text using back button or delete button, the editing does not happen. Instead i have to select and over write.

    any suggestions ?
  5. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 03 Sep 2012 Link to this post

    Hi Manoj,

    Modify the code as follows to achieve your scenario.

    Javascript:
    <script type="text/javascript">
    function OnKeyPressSerialText(sender, eventArgs)
     {
      var code = eventArgs.get_keyCode();
      if (code == 8 || code == 46 || (36 < code <41)) // Checking whether the key pressed is backspace, Delete or One of the Arrow Keys.
      {
       return;
      }
      var char = eventArgs.get_keyCharacter();
      //will allow just letters, numbers and "-" letter
      var exp = /[^a-zA-Z0-9-]/g;
      if (exp.test(char))
      {
          eventArgs.set_cancel(true);
      }
     }
    </script>

    Hope this helps.

    Thanks,
    Princy.
  6. Mohammed
    Mohammed avatar
    87 posts
    Member since:
    Aug 2011

    Posted 03 Sep 2012 Link to this post

    Thanks Princy,
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017