Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 116
  • Last Modified:

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

.345->Invalid
0
ksd123
Asked:
ksd123
  • 2
1 Solution
 
leakim971PluritechnicianCommented:
Test page : http://jsfiddle.net/sjde9z9k/
function c(n) {
    var x = "" + n;
    if(x.indexOf(".")<0) {
        x = x / 100;
    }
    return (x * 1).toFixed(2);
}

Open in new window

0
 
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?
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now