Solved

validation and formatting using javascript

Posted on 2014-11-04
3
112 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

Introduction Chart.js, used properly, can visually add a difference to your charting applications. It engages your visitors and allows them to interact with data they otherwise wouldn't be able to without expensive and complicated systems. For this…
Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
The viewer will learn how to dynamically set the form action using jQuery.
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…
Suggested Courses

623 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