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

I have a TextBox. I want to add only decimal and integer values only in that.
for that i wrote code like this

C#
function ValidateText(i) {
           if (i.value.length > 0) {
               i.value = i.value.replace(/[^\d]+/g, '');
           }
       }

But when i enter the Decimal value it's not taken the "." value. can any one help me to resolve this issue.
Posted

your regular expression itself say its going to accept only digits. so
Use this regular expression. it may help you.
C#
/^\d+\.?\d*$/
 
Share this answer
 
Comments
[no name] 30-Oct-12 7:56am    
sry it's accepting only character
Use this expression

C#
[0-9]+(\.[0-9][0-9]?)?


Check this expression in online itself.
http://www.regexplanet.com/advanced/java/index.html[^]
 
Share this answer
 
Keep it simple... Replace the text box by this control:

Numeric TextBox : Allow your users to enter numeric data the easy way[^]
 
Share this answer
 

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