validation and formatting using javascript

Hi Experts,

I want to validate and  format input value into 2 decimals  using javascript.Here is my requriement, user can enter maximum 4 digits and want to satisfy all the below scenarios.I am looking for javascript code for the below requirement

i/p         o/p

0->        .00
1->        .01
22->      .22
888->    8.88
2222->  22.22
.1->         .10
.66->      .66

Who is Participating?
leakim971Connect With a Mentor PluritechnicianCommented:
Test page :
function c(n) {
    var x = "" + n;
    if(x.indexOf(".")<0) {
        x = x / 100;
    return (x * 1).toFixed(2);

Open in new window

ksd123Author Commented:
Thank you so much and it's working fine. Actually the input is quantity field and  user can't enter more than 99.99 ie. maximum 4 digits, how can I restrict in the above logic to enter max 4 digits only?
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.