Number Text Box in C# - Pls help

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • MariaJC
    New Member
    • Feb 2007
    • 11

    Number Text Box in C# - Pls help

    Hi all,
    pls help me as i have been stuck for the past 4 days with the number alone text box that i am using in my asp.net web design.

    I need to alert the user once the focus of this txtbox is lost, with a msg box.

    I am using C# and my javascript code is:


    function CheckValue()
    {
    if (isNaN(document .getElementById ("txtNumber" ))
    {
    alert("Please enter numeric values only.");
    return false;
    }
    }

    I tried to get the id of the txtbox using
    alert(document. getElementById( "txtNumber" ));
    But the alert shows null. How do i pass the control to the javascript function?


    my asp.net code is :

    <asp:TextBox ID="txtNumber" runat="server"
    Width="31px" ToolTip="1 to 100 - numbers only" MaxLength="3" ></asp:TextBox>



    my C# code in the Page is :

    protected void Page_Load(objec t sender, EventArgs e)
    {
    txtNumber.Attri butes.Add("onbl ur", "CheckValue();" );

    }

    Please help...
  • rayapatilavakumar
    New Member
    • Jan 2007
    • 6

    #2
    hi i am giving you the code in vb script
    1) take 1 custom validator control
    2) set propeties controltovalida te - textbox id you are validating
    error message , text,give the validation function - f1
    3) now add a new item vb script file (.vbs)
    in that type the below code
    function f1(s,args)
    dim myvar,mycheck
    myvar = args.value
    mycheck= IsNumeric(MyVar )
    if mycheck = true then
    args.Isvalid= true
    else
    args.Isvalid= false
    end if
    end function
    4) finally in your page's(where the text box was presented) html view ,in <head> tag write
    <script language="VBScr ipt"></script>
    I hope it will help you

    Comment

    • kenobewan
      Recognized Expert Specialist
      • Dec 2006
      • 4871

      #3
      Have you resolved the problem?

      Comment

      • wertqt
        New Member
        • Feb 2007
        • 21

        #4
        hi,
        im not too sure what u want but im currently working on the textbox restricting numerical values only.
        This is what i written in C#.Net :
        Code:
        [CODE]string AllowChar = @"^(\d{10})$"; 
        
        if (textBox1.Length == 3 && Regex.IsMatch(textBox1.Trim(), AllowChar))
        
        {
        ..................
        }
        else
        {
        MessageBox.Show("Serial Number must be 3 characters in length and in numerical form.",
                        this.Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    
        {
        [/CODE]

        Be reminded to add this at the top of the .cs file :

        Code:
        using System.Text.RegularExpressions;
        i hope this will help u.

        Comment

        • MariaJC
          New Member
          • Feb 2007
          • 11

          #5
          Originally posted by kenobewan
          Have you resolved the problem?
          hi

          thanx

          it has been resolved


          rgds

          Comment

          Working...