Solved

validation and formatting using javascript

Posted on 2014-11-04
3
106 Views
Last Modified: 2014-11-06
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
Comment
Question by:ksd123
  • 2
3 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 40423130
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
 

Author Comment

by:ksd123
ID: 40423145
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
 
LVL 82

Expert Comment

by:leakim971
ID: 40423611
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Can't get page to reload 9 18
Compute age Html 2 23
Echo'd values in dropdowns 6 27
close pop up windows when logout or main window closes 17 38
There are a couple ways to attach a JavaScript function to dynamically created elements. You can make a new script for each element as it’s created or you can use delegation. Delegation allows a single script that is added at page creation to mat…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question