You must Sign In to post a response.
  • Category: Windows 8

    How to validate textbox in ?


    how to vaildition in textbox. does not enter a special character or number in name textbox filed
    in visual studio 2008 version.
  • #764627
    you can use Keypress event of textbox, see below snippet
    Private Sub TextBox_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox.KeyPress
    If Not Char.IsLetter(e.KeyChar) Then e.Handled = True 'ignore everything but letter keys
    End Sub
    Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
    If (Microsoft.VisualBasic.Asc(e.KeyChar) > 48) _
    Or (Microsoft.VisualBasic.Asc(e.KeyChar) < 57) Then
    e.Handled = True
    End If
    If (Microsoft.VisualBasic.Asc(e.KeyChar) = 8) Then
    e.Handled = False
    End If
    End Sub

    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]

  • #764628

    Try this code snippet.

    Private Sub txtName_Leave(sender As Object, e As System.EventArgs) Handles txtName.Leave

    'If Not A Matching Format Entered
    If Not Regex.Match(txtName.Text, "^[a-z]*$", RegexOptions.IgnoreCase).Success Then 'Only Letters

    MessageBox.Show("Please Enter Alphabetic Characters Only!") 'Inform User

    txtName.Focus() 'Return Focus
    txtName.Clear() 'Clear TextBox

    NameValid = False 'Boolean = False

    NameValid = True 'Everything Fine

    End If

    End Sub

    Or try this

    Private Sub textbox_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles textbox.KeyPress
    If Char.IsLetterOrDigit(e.KeyChar) Or Char.IsControl(e.KeyChar) Then
    e.Handled = False
    e.Handled = True
    End If
    End Sub

    Sridhar Thota.
    Editor: DNS Forum.

  • #764639

    Refer below..
    private void TextBox1_KeyPress(object sender, KeyPressEventArgs e)
    Char keypress = e.KeyChar;
    if (Char.IsLetter(keypress ))
    e.Handled = false
    e.Handled = true;

    Hope this will help you

    Never lose hope..You never know what tomorrow will bring

  • #764649
    To accept only alphabets

    <script language ="javascript">
    function Testme()
    var val = document.getElementById('<%=txtName.ClientId %>').value;
    if (!isNaN(val))
    alert("Numeric values are allowed!");
    return false;

    Asheej T K
    Microsoft MVP[ASP.NET/IIS]
    DotNetSpider MVM

Sign In to post your comments