VB.NET - Textboxt Validation - Asked By Bigboy on 14-Feb-12 03:59 AM

How can I validate a textbox such that it only allows the inputting of only digits and letters on a textbox_TextChanged event.
Thank you
smr replied to Bigboy on 14-Feb-12 04:06 AM
hi

try this

Private Sub Text1_KeyPress(KeyAscii As Integer)
Dim ch As String
 
  ch = Chr$(KeyAscii)
  If Not ( _
    (ch >= "a" And ch <= "z") Or _
    (ch >= "A" And ch <= "Z") Or _
    (ch >= "0" And ch <= "9") _
  ) Then
    ' Cancel the character.
    KeyAscii = 0
  End If
End Sub


follow
http://www.vb-helper.com/howto_allow_letters_numbers.html
Web Star replied to Bigboy on 14-Feb-12 04:06 AM
you can use keypress event of textbox for such type of validation in textbox on window form as follows

Private Sub TextBox2_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox2.KeyPress
        If (Microsoft.VisualBasic.Asc(e.KeyChar) < 65) _
            Or (Microsoft.VisualBasic.Asc(e.KeyChar) > 90) _
            <strong class="highlight">And</strong> (Microsoft.VisualBasic.Asc(e.KeyChar) < 97) _
            Or (Microsoft.VisualBasic.Asc(e.KeyChar) > 122) Then
            'Allowed space
            If (Microsoft.VisualBasic.Asc(e.KeyChar) <> 32) Then
                e.Handled = True
            End If
        End If
        ' Allowed backspace
        If (Microsoft.VisualBasic.Asc(e.KeyChar) = 8) Then
            e.Handled = False
        End If
End Sub
kalpana aparnathi replied to Bigboy on 14-Feb-12 04:09 AM
hi,

Private Sub Text1_KeyPress(KeyAscii As Integer)
  Select Case KeyAscii
    Case 65 <strong class="highlight">To</strong> 90, 48 <strong class="highlight">To</strong> 57, 8 ' A-Z, 0-9 <strong class="highlight">and</strong> backspace
    'Let these key codes pass through
    Case 97 <strong class="highlight">To</strong> 122, 8 'a-z <strong class="highlight">and</strong> backspace
    'Let these key codes pass through
    Case Else
    'All others get trapped
    KeyAscii = 0 ' set ascii 0 <strong class="highlight">to</strong> trap others input
  End Select
End Sub
Somesh Yadav replied to Bigboy on 14-Feb-12 04:14 AM

numeric fields have a Regular Expression Validation control to make sure the input value is numeric.

asp:RegularExpressionValidator, if you enter other 0-9 , it will throw error!

<input type=text runat=server id=txtNo>
    <asp:RegularExpressionValidator runat=server
        ControlToValidate="txtNo"
        ErrorMessage="Enter numbers alone."
        ValidationExpression="^[0-9]$" />

or else use keydown event if its a windows app,

private void textBoxNumber_KeyDown(object sender,   KeyEventArgs e )    
{ if (Char.IsDigit(e.KeyChar) == true )  
      {
      } 
      else  
      {      
       e.SuppressKeyPress = true;
  
      }       
}

or else,

write this code in ur form load event in .cs file
TextBox1.Attributes.Add("onkeypress","return CheckNemericValue(event);");//here textbox1 is ur server side control

then wite this javascript funciton in html

function CheckNemericValue(e)

 var key;
key = e.which ? e.which : e.keyCode;
if(key>=48 && key<=57)
{    
return true;
}
else
 {
 alert("please enter number only");
 return false;
 }
}

hope this will solve ur problem

just one more option,
if( document.getElementById('txtbox1') != null )
{
if( document.getElementById('txtbox1').value != "" )
{
var strValue = document.getElementById('txtbox1').value;
var objRegExp =/(^-?\d\d*$)/;
if( ! objRegExp.test(strValue) )
{
strMsg += "\n- Value must be numeric";
if( !errControlSet )
{
errControl = document.getElementById('txtbox1');
errControlSet = true;
}
result = false;
}
}
}
dipa ahuja replied to Bigboy on 14-Feb-12 07:03 AM

//Alphanumeric

private void textBox1_Validating(object sender, CancelEventArgs e)

{

    if (!IsAlphaNumeric(textBox1.Text))

    {

      MessageBox.Show("Enter only alphanumeric");

      textBox1.Focus();

      return;

    }

}

public bool IsAlphaNumeric(String strToCheck)

{

    Regex objAlphaNumericPattern = new Regex("[^a-zA-Z0-9]");

    return !objAlphaNumericPattern.IsMatch(strToCheck);

}