Click here to Skip to main content
15,891,859 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi,
I have numeric textfield, on paste of some text it should validate the text.
Can any one suggest me a way for this.

I have added below code to work when a user enter number,

<telerik:RadNumericTextBox ID="_rdtxtScanNumber" runat="server" MinValue="0" MaxValue="999999999999999999" NumberFormat-GroupSeparator="" NumberFormat-KeepNotRoundedValue="False" NumberFormat-DecimalSeparator=" " NumberFormat-DecimalDigits="0" Width="250px" >
<ClientEvents OnKeyPress="ValidateNumber" />
</telerik:RadNumericTextBox>

and in ValidateNumber function when user enter last key, it will validate the number and works fine. but in case of pasting it won't work
can any one suggest a way to handle this.
Posted
Updated 13-Jun-11 20:09pm
v2

You can use "onPaste" event to put validation at client side. Try this link.
Hope it helps!
 
Share this answer
 
You can use the following on textbox declaration in aspx page.

OnPaste="JavaScript:return RestrictCopyPaste();"

And use this javascript function also.

function RestrictCopyPaste()
{
    var num = clipboardData.getData('Text')
    if(parseInt(num) != num)
        return false;
}


Tested and worked for all scenarios.
 
Share this answer
 
Comments
Tarun Mangukiya 13-Jun-11 8:13am    
I agree
Handle the TextChanged event, and ensure no non-numeric characters have been pasted - if they have, remove them.
 
Share this answer
 
Comments
RakeshMeena 13-Jun-11 5:48am    
My 5.... Good one! See my answer for client side handling.
BobJanova 14-Jun-11 6:02am    
My 4 ... server side would be very annoying to use, client side (JS) is much better.
Hi,

Please use the onkeypress or onblur event for textbox.
In those events write one javascript function which will validate the entered text.

Regards,
Kiran.
 
Share this answer
 
On the text change put this codes

C#
System.Windows.Forms.TextBox txtNumOnly = (System.Windows.Forms.TextBox)sender;
if(!Char.IsDigit(txtNumOnly.Text))
{
   txtNumOnly.Text = "";
}
 
Share this answer
 
v4
Comments
Kiran Sonawane 13-Jun-11 8:44am    
Always use code block for your code
BobJanova 14-Jun-11 6:03am    
My 4 ... server side would be very annoying to use, client side (JS) is much better.
[no name] 11-Aug-11 3:06am    
The question was for ASP.NET not WinForms

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900