Results 1 to 5 of 5

Thread: Textbox validation in ASP.NET

  1. #1
    Join Date
    Nov 2009
    Posts
    72

    Textbox validation in ASP.NET

    Hello to all,
    I am last year B.Sc.I.T. student. I am working on one live project which has asp.net as front end language. I have created textbox for entering Mobile number. I want to prevent the user from entering alphabets and specials characters in a textbox. Can anyone tell me the code to prevent the user from entering alphabets and specials characters in a textbox. Please help me.
    Thanks in advanced.
    Last edited by Kasper; 19-01-2010 at 06:06 PM.

  2. #2
    Join Date
    Apr 2008
    Posts
    1,948

    Re: Textbox validation in ASP.NET

    Hey it is very easy process to prevent other user from entering alphabates in Textbox. You have to use Regular expression to do this. You can use following code to do this.

    Code:
    <asp:TextBox ID="TB1" runat="S" Style="z-index: 150; left: 260px; position: absolute;
    top: 283px" ValidationGroup="C"></asp:TB>
    
    <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="S" ControlToValidate="TB1"
    ErrorMessage="Only Numbers" Style="z-index: 110; left: 524px; position: absolute;
    top: 285px" ValidationExpression="^\d+$" ValidationGroup="C"></asp:RegularExpressionValidator>

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

    Re: Textbox validation in ASP.NET

    Following code allowed you to prevent user from entering alphabetics or any special characters.

    Code:
       
    Private Sub TB1(ByVal sender As Object, ByVal w As System.Windows.Forms.KeyPressEventArgs) Handles TB1
    If (Microsoft.VisualBasic.Asc(w.KeyChar) < 48) _Or (Microsoft.VisualBasic.Asc(w.KeyChar) > 57) Then
    w.H = True
    End If
    If (Microsoft.VisualBasic.Asc(w.KeyChar) = 8) Then
    w.H = False
    End If
    End Sub
    Last edited by Katty; 19-01-2010 at 08:10 PM.

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

    Re: Textbox validation in ASP.NET

    You can validate number using following code.
    Code:
    <script type="text/asp.net" >
    function validateNumber(e)
    {
    
        var k;
        if(window.event)
        {
            k = window.event.kCode;
        }
        else
        {
            k = e.which; 
        }
        if (!((k >47 && k <=57) || ( k ==8)|| ( k == 13) || ( k == 9) ||( k == 0)||(k ==127)))
        {
            alert("  enter numbers between 0 to 9 ");
            return false;
        }
        else
            return true;
    }
    
    function validateAlphabet(e)
    {
    
        var k;
        if(window.event)
        {
            k = window.event.keyCode;
        }
        else
        {
            k = e.which; 
        }
        
        if (!((k >96 && k <=122) || (k >64 && k <=90) || ( k ==8)|| ( k == 13) || ( k == 9) ||( k == 0)||(k==127)))
        {
            alert(" You can enter only number ");
            return false;
        }
        else
            return true;
    }
    </script>
    
    c#:
    
    protected void Page_Load(object sender, EventArgs e)
        {
            txtNumber.Attributes.Add("press1", "return validateNumber(event)");
            txtAlphabet.Attributes.Add("press1", "return validateAlphabet(event)");
        }

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

    Re: Textbox validation in ASP.NET

    I use the following code in my6 project for same purpose as yours and it works right in my project. I think you also use this code to do this. Just copy this code and save it in your project.


    Code:
    #Region "TextBox Events"
    
    Private Sub tRNo_KP(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles tRNo_KP
    Try
    If C.IsLetterOrDigit(e.KeyChar) = False And C.IsControl(e.KeyChar) = False Then
    e.Handled = True
    End If
    Catch ex As Exception
    ShowException(ex.Message, MESSAGEBOX_TITLE, ex)
    End Try
    End Sub
    #End Region

Similar Threads

  1. Replies: 2
    Last Post: 20-08-2010, 01:23 AM
  2. Numerical value on TextBox
    By KABIRA16 in forum Software Development
    Replies: 3
    Last Post: 29-10-2009, 04:25 PM
  3. How to recover a value in a textbox
    By S_Asnodkar in forum Software Development
    Replies: 4
    Last Post: 29-04-2009, 03:37 PM
  4. C#: Validation inside a Textbox?
    By RadhaV in forum Software Development
    Replies: 4
    Last Post: 10-02-2009, 07:44 PM
  5. Numeric TextBox in VB.NET
    By Gauresh in forum Software Development
    Replies: 0
    Last Post: 22-12-2008, 05:18 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,714,092,971.21592 seconds with 16 queries