RadTextBox error prompt like RadNumericTextBox

4 posts, 0 answers
  1. Nileshkumar
    Nileshkumar avatar
    18 posts
    Member since:
    May 2011

    Posted 19 Aug 2011 Link to this post

    Hi,
    I have RadTextBox in my aspx page & i want to restrict user to enter any char except aplhabet. But when user enter any other char i want to display error prompt in radtextbox same like it will display in radNumericTextBox when user alphabets.
    how can i implement like this?
  2. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 19 Aug 2011 Link to this post

    Hello Nilesh,

    You can restrict entering numbers in RadTextBox in the client event OnKeyPress as shown below. You can show a prompt like RadNumericTextBox by removing the "textbox._invalid" and calling the function updateCssClass().
    aspx:
    <telerik:RadTextBox ID="RadTextBox1" runat="server" >
        <ClientEvents OnKeyPress="keyPress" />
    </telerik:RadTextBox>
    <asp:Button ID="Button1" runat="server" OnClientClick="return OnClientClick();" Text="Alert" />

    Javascript:
    <script type="text/javascript">
      function keyPress(sender, args)
      {
            var text = sender.get_value() + args.get_keyCharacter();
            if (!text.match('^[0-9]+$'))//enter only numbers
                args.set_cancel(true);
      }
         
     function OnClientClick()
     {
       // show prompt like RadNumericTextBox
        var textbox = $find("<%= RadTextBox1.ClientID %>");
        textbox._invalid = true;
        textbox.updateCssClass();    
        return false;
     }
    </script>

    Another suggestion is to use RadNumericTextBox which has all these functionalities implemented in itself. Check the following help documentation which explains more about this.
    RadNumericTextBox Basics.

    Thanks,
    Princy.
  3. Nileshkumar
    Nileshkumar avatar
    18 posts
    Member since:
    May 2011

    Posted 19 Aug 2011 Link to this post

    Hi Princy,
    I don't want this solution. When user enters numeric value in RadTextBox that time error prompt should display like RadNumericTextBox(While enter alphabets or Special characters). 
    Please find attached snapshot " RadTextBox.png" for detail.
  4. Martin
    Admin
    Martin avatar
    833 posts

    Posted 24 Aug 2011 Link to this post

    Hello Nileshkumar,

    You can use a slightly modified version of the approach Princy suggested:

    <script type="text/javascript">
        function KeyPress(sender, args)
        {
            if (args.get_keyCharacter().search('[0-9]')>-1)
            {
                args.set_cancel(true);
                sender._invalid = true;
                sender.updateCssClass();
            }
            else
            {
                sender._invalid = false;
                sender.updateCssClass();
            }
        }
    </script>
    <telerik:RadTextBox runat="server" ID="RTB1">
        <ClientEvents OnKeyPress="KeyPress" />
    </telerik:RadTextBox>

    I hope this helps.

    Regards,
    Martin
    the Telerik team

    Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's DevProConnections Awards. We are competing in mind-blowing 20 categories and every vote counts! VOTE for Telerik NOW >>

Back to Top