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

RadTextBox error prompt like RadNumericTextBox

3 Answers 102 Views
Input
This is a migrated thread and some comments may be shown as answers.
Nileshkumar
Top achievements
Rank 1
Nileshkumar asked on 19 Aug 2011, 09:46 AM
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?

3 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 19 Aug 2011, 11:10 AM
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.
0
Nileshkumar
Top achievements
Rank 1
answered on 19 Aug 2011, 12:35 PM
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.
0
Martin
Telerik team
answered on 24 Aug 2011, 08:55 AM
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 >>

Tags
Input
Asked by
Nileshkumar
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Nileshkumar
Top achievements
Rank 1
Martin
Telerik team
Share this question
or