Input format of a Text Box

I'm net to .NET.   How can I control the format of the data input into a text box?   I want a money field.  Or just numeric digits.
LVL 1
HLRosenbergerAsked:
Who is Participating?
 
Alfred A.Commented:
0
 
intlaqaCommented:
Hi,

You can let the user just type whatever he wants and use a CompareValidator with DataTypeCheck for Money, for example:


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default5.aspx.cs" Inherits="Default5" %>

<!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></title>
</head>
<body>
    <form id="form1" runat="server">
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    <asp:Button ID="Button1" runat="server" Text="Button" />
    
    <asp:CompareValidator ID="CompareValidator1" runat="server" 
        ControlToValidate="TextBox1" ErrorMessage="Please type valid carrency value" 
        Operator="DataTypeCheck" Type="Currency"></asp:CompareValidator>
    </form>
</body>
</html>

Open in new window

0
 
HLRosenbergerAuthor Commented:
Thanks.
0
 
intlaqaCommented:
Or you can write some JS code to alow only the charachters you want, for example:


Got the Javascript code from here: http://www.go4expert.com/forums/showthread.php?t=2359
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default5.aspx.cs" Inherits="Default5" %>

<!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">
<script type="text/javascript">
    function validateCurrency(evt) {
        var e = event || evt; // for trans-browser compatibility
        var charCode = e.which || e.keyCode;

        if (charCode > 31 && (charCode < 48 || charCode > 57))
            return false;

        return true;

    }
</script>
</head>
<body>
    <form id="form1" runat="server">
    <asp:TextBox onkeypress='return validateCurrency();' ID="TextBox1" runat="server"></asp:TextBox>
    </form>
</body>
</html>

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.