don't allow user to write special characters in RadTextBox

10 posts, 0 answers
  1. Mohammed
    Mohammed avatar
    89 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
    89 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. 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 ?
  4. 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.
  5. Mohammed
    Mohammed avatar
    89 posts
    Member since:
    Aug 2011

    Posted 03 Sep 2012 Link to this post

    Thanks Princy,
  6. Wayne
    Wayne avatar
    5 posts
    Member since:
    Feb 2018

    Posted 16 Apr 2018 in reply to Mohammed Link to this post

    Sorry I could not get this to work. Not calling the JS script. Is this also out of date as you don't have OnKeyPress its now ClientEvents-OnKeyPress.

    Can you show this sample again with one aspx file and one js file.

    Thank you

     

  7. Eyup
    Admin
    Eyup avatar
    3893 posts

    Posted 19 Apr 2018 Link to this post

    Hello Wayne,

    Please try changing the if condition as follows:
    if (code == 8 || code == 46 || (36 < code && code < 41))

    I am also sending a sample working web site as requested.

    Regards,
    Eyup
    Progress Telerik
    Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
  8. Majeed
    Majeed  avatar
    7 posts
    Member since:
    Mar 2014

    Posted 28 Jul 2018 in reply to Mohammed Link to this post

    Hi Mohammed,

    this is not secured validation because If we disable the javascript from client browser than it will allow to enter restricted characetr in textbox

  9. Majeed
    Majeed  avatar
    7 posts
    Member since:
    Mar 2014

    Posted 28 Jul 2018 in reply to Eyup Link to this post

    Hello Eyup,

    I am facing some problem like Using Javascript for validation to restrict some special characters but one of our end user entered special characters by stopping/disabled the javascript from client browser.

    kindly suggest me any better solution.

     

    Thanks,

    Majeed Khan

  10. Eyup
    Admin
    Eyup avatar
    3893 posts

    Posted 02 Aug 2018 Link to this post

    Hi Majeed,

    Generally, for numeric input we suggest that RadNumericTextBox is used:
    https://demos.telerik.com/aspnet-ajax/numerictextbox/overview/defaultcs.aspx

    As for restricting some special symbols with RadTextBox, you can use javascript as with regular asp:TextBox. If this is not applicable for a specific user, I'm afraid this is not specifically related to Telerik and you can check discussions over the net for regular asp:TextBoxes or inputs on this matter and once you manage to achieve that, then you can use a similar logic for the RadTextBox as well.

    Regards,
    Eyup
    Progress Telerik
    Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Back to Top