hi everyone.
can someone teach me how to validate the textbox in vb.net?
that the textbox can only accept numeric only.
and the other textbox can only accept alphanumeric only.
much thanks.
try this code:
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
e.Handled = NumbersOnly(e.KeyChar, TextBox1)
End Sub
Private Function NumbersOnly(ByVal pstrChar As Char, ByVal oTextBox As TextBox) As Boolean
'validate the entry for a textbox limiting it to only numeric values and the decimal point
If (Convert.ToString(pstrChar) = "." And InStr(oTextBox.Text, ".")) Then Return True 'accept only one instance of the decimal point
If Convert.ToString(pstrChar) <> "." And pstrChar <> vbBack Then
Return IIf(IsNumeric(pstrChar), False, True) 'check if numeric is returned
End If
Return False 'for backspace
End Function
Try the following code for alphanumerics only
protected override void OnKeyDown(KeyEventArgs e)
{
Keys k = e.KeyCode;
if (k == Keys.Back)
{
this.isBack = true;
this.isAlphaNum = false;
}
else
{
bool isAlpha = !e.Alt && !e.Control && (k >= Keys.A && k <=
Keys.Z);
// Determine whether the keystroke is a number
bool d0d9 = k >= Keys.D0 && k <= Keys.D9;
bool numpad = k >= Keys.NumPad0 && k <= Keys.NumPad9;
bool isNumber = (!e.Alt && !e.Control && !e.Shift) && (d0d9
|| numpad);
this.isBack = false;
this.isAlphaNum = isAlpha || isNumber;
}
base.OnKeyDown(e);
}
protected override void OnKeyPress(KeyPressEventArgs e)
{
if (!this.isBack && !this.isAlphaNum)
{
e.Handled = true;
return;
}
char c = e.KeyChar;
if (this.isBack && this.SelectionStart > 0)
{
int currentPosition = SelectionStart;
this.Text = this.Text.Substring(0, this.TextLength - 1);
this.SelectionStart = currentPosition - 1;
e.Handled = true;
return;
}
if (this.isAlphaNum && this.SelectionStart < this.MaxLength)
{
int currentPosition = SelectionStart;
this.Text = this.Text.Insert(currentPosition,
Char.ToUpper(c).ToString());
this.SelectionStart = currentPosition + 1;
e.Handled = true;
return;
}
base.OnKeyPress(e);
}
try this code:
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress e.Handled = NumbersOnly(e.KeyChar, TextBox1) End Sub Private Function NumbersOnly(ByVal pstrChar As Char, ByVal oTextBox As TextBox) As Boolean 'validate the entry for a textbox limiting it to only numeric values and the decimal point If (Convert.ToString(pstrChar) = "." And InStr(oTextBox.Text, ".")) Then Return True 'accept only one instance of the decimal point If Convert.ToString(pstrChar) <> "." And pstrChar <> vbBack Then Return IIf(IsNumeric(pstrChar), False, True) 'check if numeric is returned End If Return False 'for backspace End Function
is "e" a class? where should i declare it? thanks
is "e" a class? where should i declare it? thanks
e is event
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.