Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
46
rated 0 times [  50] [ 4]  / answers: 1 / hits: 5346  / 10 Years ago, wed, april 9, 2014, 12:00:00

I have to validate a price textbox field with below conditions using javascript/jquery :



Please help me out to create a jquery for below validations :




  • The 'Price' field will take in a negative value.


  • 'Price' field should only display to the second decimal (and only allow a user to enter to the second decimal).


  • 'Price' field should not be allowed to accept alphabetic characters.


  • 'Price' field will accept multiple decimals. This value can be saved. (Multiple decimal is allowed,Regular Decimal not allowed after each decimal there can be only 2 digits).




Thanks a Ton.


More From » jquery

 Answers
16

You can also use regular expression to validate the value of price



^-?(d{1,3})(.d{1,2})?$


This regex will accept negative as well as positive values with numbers upto 1 to 999 and also with 2 decimal places



You modify the regex as per your requirement.



d{1,3} - will accept upto 3 digits



.d{1,2} - will accept decimal digits upto 2 places



EDIT



This regex will allow 999.99.99.99 or 999.99.99 type values



^-?(d{1,3})(.d{1,2})*?$


Explanation



(.d{1,2})* - This will accept dot multiple times

[#46184] Tuesday, April 8, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
hayleevalenciac

Total Points: 164
Total Questions: 89
Total Answers: 106

Location: Burkina Faso
Member since Thu, Dec 15, 2022
2 Years ago
hayleevalenciac questions
Thu, Jan 23, 20, 00:00, 4 Years ago
Tue, Jan 7, 20, 00:00, 5 Years ago
Thu, Oct 17, 19, 00:00, 5 Years ago
;