only allow numbers and fullstop (period) in textbox
I know that the following code blocks the usser from ussing spaces in a textbox however how do i allow the user to only use numbb开发者_JAVA技巧ers and a fulstop (so i can add values like 1.5)
Private Sub Textbox4_keyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox4.KeyDown
If e.KeyCode = Keys.Space Then
TextBox4.Clear()
MsgBox("Invalid character. No spaces Permited...")
End If
From a usability point of view, testing for valid input in the KeyDown
event isn’t good. For example, what happens when the user wants to paste text into your text box?
Furthmore, the user can still paste invalid input using the TextBox’ context menu, your code won’t notice this.
You should allow all input, and then test for validity when the user leaves the text box. VB has an extra event for this, which is fired whenever the text box loses focus: Validating
. Use this event (and only this event) to test for valid input.
A simple approach for this might be to look for the "allowed" characters, if not one of them, show the error message.
In last 20years of writing code I always use the following rationale for TextBoxes Check Characters.
First you have to create a separate Class which you may call it (for your convenience) Char_Validation.
Inside to this Class you’ll put a Function which returns Boolean as follows .
Public Class Char_Validation
Public Const Gr As String = "Greek"
Public Const En As String = "English"
Public Const Num As String = "Numbers"
Public Const FullGr As String = "Full Greek"
Public Const FullEn As String = "Full English"
Public Const EnN As String = "English with Numbers"
Public Const GrN As String = "Greek with Numbers"
Public Shared Function ValidateChar(ByVal AsciiChar As String, ByVal CharTable As String, ByVal sender As Object, ByVal e As System.EventArgs) As Boolean
Dim ConvChar As Integer = CUInt(Microsoft.VisualBasic.Asc(AsciiChar))
Dim ConvCharW As Integer = CUInt(Microsoft.VisualBasic.AscW(AsciiChar))
ValidateChar = False
Select Case CharTable
Case En
Select Case ConvChar
Case 65 To 126, 145 To 150, 8, 32 To 47, 58 To 64, 128, 130
ValidateChar = True
End Select
Case EnN
Select Case ConvChar
Case 48 To 57, 65 To 126, 8, 32, 45
ValidateChar = True
End Select
.
.
.
.
.
Case Num
Select Case ConvChar
Case 44 To 57, 92, 8
ValidateChar = True
End Select
End Select
End Function
End Class
At your Class in Form you will use the TextBox_KeyPress on which you’ll use the following code.
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
ErrorProvider1.Clear()
ErrorLabel.ForeColor = Drawing.Color.Black
Select Case Char_Validation.ValidateChar(e.KeyChar, Char_Validation.Num, sender, e)
Case True
Case False
ErrorProvider1.SetError(TextBox1, "Wrong Character Only Numbers")
Beep()
e.KeyChar = ""
End Select
End Sub
Thus you will prohibit the user to place characters out of your decision.
I hope that will cover you from now.
Following code worked for me on : firefox, IE 8, chrome, Safari and iphone.
function dotplaced(myfield){
if(myfield.indexOf(".")===-1){
return false;
}
return true;
}
function NumbersOnly(myfield, e) {
var key;
var keychar;
if (window.event) {
key = window.event.keyCode;
}
else if (e) {
key = e.which;
}
else {
return true;
}
keychar = String.fromCharCode(key);
// control keys
if ((key == null) || (key == 0) || (key == 8) ||
(key == 9) || (key == 13) || (key == 27)) {
return true;
}
// numbers
else if ((("0123456789").indexOf(keychar) > -1)) {
return true;
}
// decimal point jump
else if (!dotplaced(myfield.value) && (keychar == ".")) {
//myfield.form.elements[dec].focus();
return true;
}
else {
return false;
}
}
精彩评论