Solved

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

Posted on 2010-09-20
11
2,769 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
[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
  • 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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 

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
 
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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

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 article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
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…
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