Solved

validation and formatting using javascript

Posted on 2014-11-04
3
98 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer …
In this article you'll learn how to use Ajax calls within your CodeIgniter application. To explain this, I'll illustrate how to implement a simple contact form to allow visitors to send you an email through your web site.
The viewer will learn how to dynamically set the form action using jQuery.
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)

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now