Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Register Tags Active Topics RSS Search Mark Forums Read SiteMap

Tags: , , , , ,

Sponsored Links



Textbox validation in ASP.NET

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 19-01-2010
Member
 
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.
Reply With Quote
  #2  
Old 19-01-2010
Praetor's Avatar
Member
 
Join Date: Apr 2008
Posts: 1,937
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>
Reply With Quote
  #3  
Old 19-01-2010
Member
 
Join Date: May 2008
Posts: 1,990
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.
Reply With Quote
  #4  
Old 19-01-2010
kelfro's Avatar
Member
 
Join Date: Apr 2008
Posts: 1,976
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)");
    }
Reply With Quote
  #5  
Old 19-01-2010
Zecho's Avatar
Member
 
Join Date: May 2008
Posts: 2,267
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
Reply With Quote
Reply

  TechArena Community > Software > Software Development


Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Textbox validation in ASP.NET"
Thread Thread Starter Forum Replies Last Post
Excel/VBA: Copying text from UserForm Textbox to a Worksheet Textbox Neil Ives Software Development 2 20-08-2010 02:23 AM
Moving from one TextBox to another with TAB GeforceUser Software Development 3 05-11-2009 06:31 PM
Numerical value on TextBox KABIRA16 Software Development 3 29-10-2009 05:25 PM
C#: Validation inside a Textbox? RadhaV Software Development 4 10-02-2009 07:44 PM
Validation Incomplete: Unable to Perform Validation [0x80080299] Waiting to get hacked Windows Update 2 09-07-2006 07:27 PM


All times are GMT +5.5. The time now is 11:20 AM.