Click here to Skip to main content
15,896,557 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

Im develpoing a web application.. i want to allow numbers in textbox ..
This is the code which im using in aspx for validtaion but couldn't get the result ..
can any one help me to solve this...

Thank you

C#
<asp:RegularExpressionValidator ID ="RegularExpressionValidator_CardCode" runat="server" ControlToValidate="RadTb_CardCode" ErrorMessage="CardCode Invalid(Numbers Only)" Display="Dynamic" ValidationExpression="^\d*"></asp:RegularExpressionValidator>
Posted
Comments
Member 11148509 2-Sep-15 10:52am    
Thanks all of u for ur suggestions..i have small problem with my code while working with regularexpressions and i solved that ..
Thank u

Hello ,
Change the ValidationExpression from
ValidationExpression="^\d*"

to
ValidationExpression="^\d+$"
 
Share this answer
 
Comments
Andy Lanng 2-Sep-15 5:41am    
depending on the version of .net, you can change the asp:textbox textmode to Number so the user can't enter anything but numbers. I'm not sure how good it is with decimals though
Member 11148509 2-Sep-15 5:42am    
ya i have tried that but dint work out ...i have small doubt..i have entered characters in textbox so it should immediately tell only numeric are allowed ...can i get that by using this regularexpression ???actually im doing using telerik and using radtextbox...
Andy Lanng 2-Sep-15 5:44am    
Ha - RadNumericTextbox ^_^
RadNumericTextbox Documentation
You can also set currency, decimals etc
Member 11148509 2-Sep-15 5:44am    
textmode is used for displaying mutliline or singleline ryt??? and im using VS 2013
Andy Lanng 2-Sep-15 5:45am    
it depends on the version of .Net
html5 uses textmode so it only "looks" available in .Net4+, but you can still use it in previous versions.
They expanded on the textmodes available greatly. If the browser supports it then you can use it ^_^
I have a textbox for card details which not only validates the card on each digit entered but also identifies the provider. I used jQuery to do this and prevent NaN's from being entered

JavaScript
$(function(){ //on doc.ready
    $('.numeric').keypress(function(event) {
        // keycode 8 is backspace
        if (event.which != 8 && isNaN(String.fromCharCode(event.which))) {
            event.preventDefault(); //stop character from entering input
        }
    }
}


Now just add the class numeric to your control and voila!

If you're not using jQuery, start using it ^_^

Hope that helps
Andy
 
Share this answer
 
Comments
Andy Lanng 2-Sep-15 8:15am    
gah - at last my post actually worked >_<
Member 11148509 2-Sep-15 8:18am    
Actually mine is web application ...so its better to use textchanged event or should use in aspx .....anyways i will try ur post hope it works
Andy Lanng 2-Sep-15 8:20am    
Mine is also a web application.
you can't use textchanged event. It'll post back every type the user types.
It doesn't matter if it's a web app. You should perform these kind of things client side, ie.e javascript / jquery
Member 11148509 2-Sep-15 8:24am    
Thank u and can i know why this regularexpression is not working???
Andy Lanng 2-Sep-15 8:28am    
Check Animesh Datta's solution.
^ = begin text
\d = digit
* = zero or more.
So if you have a string "this is NaN"
The start of the string has zero or more digits so it is valid.
If you end with $:
$ = end text
the "^\d*$" means between the start AND end only zero or more digits are allowed
I hope that makes sense ^_^

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