Solved

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

Posted on 2010-09-20
11
2,510 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
ID: 33721406
you want to do thing automatically when the user press a key or left the field ?
0
 

Author Comment

by:Saroj13
ID: 33727244
yes when user press the key on the currency feld
0
 
LVL 82

Expert Comment

by:leakim971
ID: 33727298
>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
ID: 33727340
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
ID: 33727472
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 82

Expert Comment

by:leakim971
ID: 33727479
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
ID: 33728764
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
ID: 33734291
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
ID: 33736464
validation when the user left the input (ok to validate format)
0
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 33737864
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
ID: 33853227
Thanks for the points!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
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…

895 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

14 Experts available now in Live!

Get 1:1 Help Now