Results 1 to 5 of 5

Thread: IsNumeric - Web Developer

  1. #1
    Join Date
    Apr 2010
    Posts
    27

    IsNumeric - Web Developer

    I want to make a webshop in a college project. It will have a text box where you can choose how many you want to order from the list that appears. My problem lies at a point where if a user enters a letter instead of a number, I want that an error message should appear. However I do not get the error on the page. How do I do this? Can not find it with google, unfortunately.

  2. #2
    Join Date
    May 2008
    Posts
    2,389

    Re: IsNumeric - Web Developer

    Code:
    int number;
    if ( Integer.TryParse(textbox.Text, out number) {
        // All is well
    } else {
        //This is not an integer, error handling here
    }

  3. #3
    Join Date
    Nov 2008
    Posts
    1,192

    Re: IsNumeric - Web Developer

    You can also use Ajax control toolkit's filtered textbox extender. Here is the code that you can use to create a popup with the error to the user if the validation fails:

    Code:
    private void MessageBox(string message)
    {
        this.RegisterStartupScript(this, this.GetType(), "err_msg", "alert('" + message + "');", true);
    }

  4. #4
    Join Date
    Apr 2008
    Posts
    2,005

    Re: IsNumeric - Web Developer

    Although I use an event and regex so that it is not possible to use other than numbers and backspace, if the user tries to type anything, nothing happens.

    Code:
    private void textBox_privatestoresell_quantity_KeyPress(object sender, KeyPressEventArgs e)
    {
        if ((!System.Text.RegularExpressions.Regex.IsMatch(e.KeyChar.ToString(), "\\d+"))&&(!System.Text.RegularExpressions.Regex.IsMatch(e.
    KeyChar.ToString(), "\b" )))
            e.Handled = true;
    
    }

  5. #5
    Join Date
    May 2008
    Posts
    2,012

    Re: IsNumeric - Web Developer

    I created a text box for this once.

    Code:
    using System;
    using System.ComponentModel;
    using System.Drawing;
    using System.Windows.Forms;
    using System.Text.RegularExpressions;
    
    namespace orsaDB
    {
        public class CheckedTextEditor : TextBox
        {
                protected Regex m_reg;
                protected string m_reg_exp;
                protected bool m_ignore_case;
                protected bool m_allow_invalid_text;
    
                [DefaultValue(false)]
                public bool IgnoreCase
                {
                        get
                        {
                                return m_ignore_case;
                        }
                        set
                        {
                                m_ignore_case = value;
                                RebuildRegex();
                        }
                }
    
                [DefaultValue(false)]
                public bool AllowInvalidText
                {
                        get
                        {
                                return m_allow_invalid_text;
                        }
                        set
                        {
                                m_allow_invalid_text = value;
                        }
                }
                [DefaultValue(true)]
                public bool IsTextValid
                {
                        get
                        {
                                if ( m_reg != null )
                                        return ValidateText( Text );
                                else
                                        return true;
                        }
                }
    
                protected void RebuildRegex()
                {
                        m_reg = new Regex(m_reg_exp, m_ignore_case ? RegexOptions.IgnoreCase : RegexOptions.None);
                }
                [DefaultValue("")]
                public string RegularExpression
                {
                        get
                        {
                                return m_reg_exp;   
                        }
                        set
                        {
                                m_reg_exp = value;
                                m_reg = new Regex( m_reg_exp );
    
                                RebuildRegex();
                        }
                }
    
                public CheckedTextEditor()
                {
                        m_reg_exp = "";
                        m_reg = null;
                }
    
                protected override void onkeypress( KeyPressEventArgs e )
                {
                        base.onkeypress( e );
                        if ( e.KeyChar != (char)8 && !m_allow_invalid_text)
                        {
                                string text = Text + e.KeyChar;
    
                                e.Handled = !ValidateText( text );
                        }
                }
                protected bool ValidateText( string text )
                {
                        Match m = m_reg.Match( text );
                        return (m.Index == 0) && (m.Length == text.Length);
                }
    
                protected override void OnTextChanged( EventArgs e )
                {
                        ForeColor = IsTextValid ? SystemColors.WindowText : Color.Red;
                        base.OnTextChanged( e );
                }
                    /// <summary> 
                    /// Required designer variable.
                    /// </summary>
                    private System.ComponentModel.IContainer components = null;
    
                    /// <summary> 
                    /// Clean up any resources being used.
                    /// </summary>
                    /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
                    protected override void Dispose( bool disposing )
                    {
                            if ( disposing && ( components != null ) )
                            {
                                    components.Dispose();
                            }
                            base.Dispose( disposing );
                    }
    
                    #region Component Designer generated code
    
                    /// <summary> 
                    /// Required method for Designer support - do not modify 
                    /// the contents of this method with the code editor.
                    /// </summary>
                    private void InitializeComponent()
                    {
                            components = new System.ComponentModel.Container();
                    }
    
                    #endregion
    
        }
    }

Similar Threads

  1. Replies: 7
    Last Post: 04-01-2012, 09:14 PM
  2. Software developer in BPO
    By Apollos in forum Education Career and Job Discussions
    Replies: 1
    Last Post: 28-11-2009, 11:44 AM
  3. The road to a Web Developer
    By One thought in forum Education Career and Job Discussions
    Replies: 5
    Last Post: 12-02-2009, 12:52 PM
  4. IE Web Developer Toolbar
    By Stephanatic in forum Technology & Internet
    Replies: 2
    Last Post: 01-08-2008, 07:11 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,750,557,055.44686 seconds with 16 queries