Click here to Skip to main content
15,900,258 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i want to need reqular expression for quantity.
how to write regular expression for only number .
help me
Posted

Hi,
If you want to make sure the text contains only digits use a simple ^\d+$ or ^\s*\d+\s*$ to allow some spaces at the beginning and end.
To allow negative numbers:^-?\d+$ or ^[+-]?\d+$ to allow numbers like +12
For decimal numbers: ^[+-]?\d+(\.\d+)?$ (this will allow 0.54 but not .54)
This one will allow things like .54
^[+-]?(\d+(\.\d+)?|\.\d+)$
 
Share this answer
 
Comments
VJ Reddy 11-Jun-12 11:31am    
Good explanation. 5!
Jαved 12-Jun-12 7:57am    
Thank you.
Use the following :
\d+


Checkout Expresso Regex Tester : http://www.ultrapico.com/[^]
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 8-Jun-12 11:45am    
Useful tool, apparently; a 5.
--SA
Mehdi Gholam 8-Jun-12 12:37pm    
Thanks Sergey!
VJ Reddy 11-Jun-12 11:30am    
Good answer. 5!
Mehdi Gholam 11-Jun-12 12:28pm    
Thanks VJ!
try this: ^[0-9]+$

Look at this [^]tool. It is quite handy for programmers :)
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 8-Jun-12 11:44am    
Again, would work; and a good advice, a 5.
--SA
Manas Bhardwaj 8-Jun-12 12:33pm    
Thx SA!
VJ Reddy 11-Jun-12 11:30am    
Good answer. 5!
Manas Bhardwaj 12-Jun-12 1:56am    
thx! :)

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