tags:

views:

45

answers:

1

Hi all i want one validation in javascript that a text boxt should allow to enter only numeric vales in following format

4.25 4.50 2.00 likewise.there sholud be always 2 digit after decimal point and 1 digit before decimal point. if user enters 2 it sholud display 2.00 like this is my requirement.plz help me

A: 

For having only 2 digits after . you can use Number().toFixed(2) function in javscript

var num = Number(4).toFixed(2) will give you 4.00

var num = Number(4.1).toFixed(2) will give you 4.10

if you do not have number in place after ".", then toFixed will append 0 to your number.

For you second question, probably you can split your number on "." and check the first element is of length 1.

Sachin Shanbhag