Wrox Programmer Forums
|
Classic ASP Basics For beginner programmers starting with "classic" ASP 3, pre-".NET." NOT for ASP.NET 1.0, 1.1, or 2.0
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Classic ASP Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 3rd, 2007, 10:44 AM
Authorized User
 
Join Date: Nov 2007
Posts: 67
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Hughesie78
Default numeric only textbox

How can i make sure a user can only enter numerical data onto a text box(including a decimal point).
__________________
Thank You
 
Old December 3rd, 2007, 10:46 AM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

Use a regular expression.

http://www.google.com/search?source=...lar+expression
http://www.google.com/search?hl=en&s...lar+expression

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
================================================== =========
.: Wrox Technical Editor :.
Wrox Books 24 x 7
================================================== =========
 
Old December 4th, 2007, 10:19 AM
Authorized User
 
Join Date: Nov 2007
Posts: 67
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Hughesie78
Default

Here is my aspx code, I have 3 text boxes, the 2nd and 3rd will calculate the total on the text changed event, the first one will when there is no ‘onkeyup’ event, howvere the onkeyup event is used to make sure that no text is entered, how can I make it both events work?>

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

<!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>total</title>

        <script type="text/javascript" language="javascript">
function ValidateText(i)
{
    if(i.value.length>0)
    {
    i.value = i.value.replace(/[^\d]+/g, '');
    }
}
</script>
</head>
<body>
    <form id="form1" runat="server">
<asp:Button runat=server ID=btnd />
        <br />
        <br />
        Value:
        <asp:TextBox ID="TextBox1" onkeyup = "ValidateText(this)" runat="server" OnTextChanged="TextBox1_TextChanged" AutoPostBack=true>0.00</asp:TextBox>
        <asp:CompareValidator ID="cfvTest" runat="server" ControlToValidate="TextBox1" ErrorMessage="* Must be numeric"
            SetFocusOnError="True" Type="Double" Width="168px"></asp:CompareValidator>
        <br />
        Value:
        <asp:TextBox ID="TextBox2" runat="server" OnTextChanged="TextBox1_TextChanged" AutoPostBack=true>0.00</asp:TextBox>
        <asp:CompareValidator ID="CompareValidator1" runat="server" ControlToValidate="TextBox2"
            ErrorMessage="* Must be numeric" SetFocusOnError="True" Type="Double" Width="168px"></asp:CompareValidator>
        <br />
        Value:
        <asp:TextBox ID="TextBox3" runat="server" OnTextChanged="TextBox1_TextChanged" AutoPostBack=true>0.00</asp:TextBox>
        <asp:CompareValidator ID="CompareValidator2" runat="server" ControlToValidate="TextBox3"
            ErrorMessage="* Must be numeric" SetFocusOnError="True" Type="Double" Width="168px">
        </asp:CompareValidator>
        <br />
        Total &nbsp;&nbsp; :<asp:Label ID="lbl_1" runat="server" Width="144px"></asp:Label>
        <asp:TextBox ID=txt_total runat=server></asp:TextBox>
    </form>
</body>
</html>
 
Old December 4th, 2007, 01:21 PM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

TextChanged will not be raised until the Text has changed in the control AND the control loses focus.

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
================================================== =========
.: Wrox Technical Editor :.
Wrox Books 24 x 7
================================================== =========
 
Old December 5th, 2007, 08:00 AM
Authorized User
 
Join Date: Nov 2007
Posts: 67
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Hughesie78
Default

ok, can you suggest any other way around this apart from using 'ontextchanged'?
 
Old December 5th, 2007, 10:01 AM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

Ya, I would use a RegularExpressionValidator Server control. I don't understand what you are trying to do with this though:

<asp:CompareValidator ID="cfvTest" runat="server" ControlToValidate="TextBox1" ErrorMessage="* Must be numeric"
            SetFocusOnError="True" Type="Double" Width="168px"></asp:CompareValidator>

you are not comparing TextBox1 to anything. In any event, as I said, you could use a regularExpressionValidator to enforce your RegularExpression. (The method you have demonstrated above can easily be gotten around if I come to your website and disable JavaScript)

hth.

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
================================================== =========
.: Wrox Technical Editor :.
Wrox Books 24 x 7
================================================== =========
 
Old December 5th, 2007, 10:17 AM
Authorized User
 
Join Date: Nov 2007
Posts: 67
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Hughesie78
Default

 Hi, The regular expression works fine, it causes an error when text is entered, BUT…. My real problem is that I have a method that is fired from the ontextchanged event, this method calculates a running total, and displays it in another control, works fine when I have entered numbers, but when I enter text, it causes and error before it gets to display the validation error… here is my code…
 Aspx:
 <asp:TextBox ID="TextBox4" runat="server" OnTextChanged="TextBox4_TextChanged" AutoPostBack=true></asp:TextBox> <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="TextBox4" ErrorMessage="* Please input a number!" ValidationExpression="^\d{1,7}(\.\d{1,2})?$"></asp:RegularExpressionValidator>

Codebeihnd:

protected void TextBox4_TextChanged(object sender, EventArgs e)
  {
   double total = double.Parse(TextBox4.Text) ;
      lbl_1.Text = total.ToString();
    txt_total.Text = total.ToString();
  }

Is there anyway around that that I can prevent the TextBox4_TextChanged method being called if the field has text in it withou having to add a button which only caclulates after all validations are done, I want it to calculate a running total…
 
Old December 5th, 2007, 10:58 AM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

I would do something like this since you want to rely on server events:
Code:
//matches a floating point number in the 
//format N.DD
public bool isFloat(string Value)
{
   System.Text.RegularExpressions.Regex ex = new Regex("^([0-9]+(\\.[0-9][0-9]))$");
   return ex.Match(Value).Success;
}
protected void TextBox_TextChanged(object sender, EventArgs e)  
{
   TextBox txt = (TextBox)sender;
   if(!isFloat(txt.Text))
   {
     return;
   }
   else
   {      
   double total = double.Parse(txt.Text) ;   
      lbl_1.Text = total.ToString();    
    txt_total.Text = total.ToString();  
   } 
  }
I modified your TexTChanged code so that you only need one event to handle all of your textboxes textchanged event hence the line:
TextBox txt = (TextBox)sender;

hth


================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
================================================== =========
.: Wrox Technical Editor :.
Wrox Books 24 x 7
================================================== =========





Similar Threads
Thread Thread Starter Forum Replies Last Post
From numeric to alfa numeric ebekir XSLT 1 August 10th, 2007 06:13 AM
HELP!! Numeric Textbox value quentinuys HTML Code Clinic 3 December 18th, 2006 08:04 AM
Adding Numeric TextBox Values virajoza HTML Code Clinic 1 December 18th, 2006 07:59 AM
numeric format for a textbox Rudner Beginning VB 6 1 November 9th, 2004 02:29 PM
Masked TextBox & formatting TextBox melvik C# 1 September 22nd, 2003 11:01 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.