Solved

Currency validation textbox in asp.net, c# and javascript

Posted on 2010-09-20
11
2,462 Views
Last Modified: 2012-05-10
<input type="text"  value="" id="txtAmt"    runat="server"  />

I want the user will enter the currency in this format only
1.  six digits on the left of the decimal ex  123456.78
2. 2 digits on the right of the decimal.
3. if user enters 6 digits, then automatically decimal and two zeros at the end.    123456.00
4. if user enters 6 digits and then decimal and one number, then automatically one zero at the end  123456.80
user needs to enter six digits before the decimal and two after decimal everytime.

I am using asp.net, c#, javascript.

Thanks
0
Comment
Question by:Saroj13
  • 7
  • 4
11 Comments
 
LVL 82

Expert Comment

by:leakim971
Comment Utility
you want to do thing automatically when the user press a key or left the field ?
0
 

Author Comment

by:Saroj13
Comment Utility
yes when user press the key on the currency feld
0
 
LVL 82

Expert Comment

by:leakim971
Comment Utility
>yes when user press the key on the currency feld

you press 6 and you get : 123456
you automactically complete and get : 123456.00
now you press backspace now because you want number after the decimal
what happen ?

you want the program accept and let 123456.0
or (by default) complete again with a 0 : 123456.00

?
0
 

Author Comment

by:Saroj13
Comment Utility
I will just enter 6 digits on the left side of the decimal and two digits on the right of the decimal every time.
if i just enter 6 digits, it will automatically place decimal and two zeros.

if i enter 123456.2, then automatically it will add one zero at the end means 123456.20

if user enters 123456.23, then user will not enter third digit after the decimal.

user can delete the digit by left arrow, right arrow,, by select the complete num,ber, tab also should work..
0
 
LVL 82

Expert Comment

by:leakim971
Comment Utility
Use :

    function formatNumber(input) {
        var key = (window.event) ? (window.event.keyCode) : (event.which);
        if (key != 8 && key != 37 && key != 39) {
            var val = input.value.replace(/\D/g, "");
            if (val.length == 6) val = val + ".00";
            else if (val.length == 7) val = val.substr(0, 6) + "." + val.substr(6) + "0";
            input.value = val;
        }
    }

With :


<input type="text" id="txtAmt" onkeyup="formatNumber(this);" />

Open in new window

0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 82

Expert Comment

by:leakim971
Comment Utility
test page (vs2010) :

<%@ Page Title="Page d'accueil" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
<script language="javascript" type="text/javascript">
    function formatNumber(input) {
        var key = (window.event) ? (window.event.keyCode) : (event.which);
        if (key != 8 && key != 37 && key != 39) {
            var val = input.value.replace(/\D/g, "");
            if (val.length == 6) val = val + ".00";
            else if (val.length == 7) val = val.substr(0, 6) + "." + val.substr(6) + "0";
            input.value = val;
        }
    }
</script>
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<input type="text" id="txtAmt" onkeyup="formatNumber(this);" />
</asp:Content>

Open in new window

0
 

Author Comment

by:Saroj13
Comment Utility
there is no validation for six digits on the left side of the decimal. its not throwing the error if its less than 6 digits. Is it possible that we can make the input field like six dashes decimal two dashes. when user writes, dashes will be gone
0
 
LVL 82

Expert Comment

by:leakim971
Comment Utility
if you want to validate for six digit you can't use this validation when the user press a key
if you don't use validation when user press a key you can't use completion ( add dot, 00 or 0)

so choose a validation method :
validation when the user left the input (ok to validate format)
or validation when user press keys (no validation of the format or error message (alert))
0
 

Author Comment

by:Saroj13
Comment Utility
validation when the user left the input (ok to validate format)
0
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
Comment Utility
Try this one :


<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
<script language="javascript" type="text/javascript">
    function formatNumber(input) {
        var val = input.value.replace(/\D/g, "");
        if (val.length == 6) val = val + ".00";
        else if (val.length == 7) val = val.substr(0, 6) + "." + val.substr(6) + "0";
        input.value = val;
        if(!/\d{6}.\d{2}/g.test(val)) {
            alert("bad input !");
            input.value = "";
        }
    }
</script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <input type="text" id="txtAmt" onblur="formatNumber(this);" />
        <input type="text" />
    </div>
    </form>
</body>
</html>

Open in new window

0
 
LVL 82

Expert Comment

by:leakim971
Comment Utility
Thanks for the points!
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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
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…

762 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

9 Experts available now in Live!

Get 1:1 Help Now