|
-
Jul 18th, 2003, 02:30 AM
#1
Thread Starter
Junior Member
Textbox Validation question
Hello guys, I wrote this code to validate a textbox... What I want is that the textbox only receive value with 0 or greater or null... The code I wrote below only work with value 0 or greater but it wouldnt' work when I leave the textbox null. Please advise what to do. Thank you.
Private Function isValidPrice2(ByVal Price2 As String) As Boolean
Dim Result As Boolean = False
If IsNumeric(Price2) Then
If Price2 > 0.0 Or Price2 = "null" Then
If CType(Price2, Double) = Price2 Then
Result = True
End If
End If
End If
Return Result
End Function
-
Jul 18th, 2003, 03:05 AM
#2
Addicted Member
hi
try this
Private Function isValidPrice2(ByVal Price2 As String) As Boolean
Dim Result As Boolean = False
If IsNumeric(Price2) Then
If Price2 > 0.0 Or Price2 = vbnullstring Then
If CType(Price2, Double) = Price2 Then
Result = True
End If
End If
End If
Return Result
End Function
To follow the path:
look to the master,
follow the master,
walk with the master,
see through the master,
become the master.-ZEN
-
Jul 18th, 2003, 03:43 AM
#3
Thread Starter
Junior Member
Thank you for replying... I tried that and still didnt' work... By the way, I am using Visual Basic.net
-
Jul 18th, 2003, 04:58 PM
#4
Thread Starter
Junior Member
Help
Please advise what I need to do.. Thank You
-
Jul 18th, 2003, 09:47 PM
#5
Sleep mode
Try :
VB Code:
Private Function isValidPrice2(ByVal Price2 As String) As Boolean
Dim Result As Boolean = False
If IsNumeric(Price2) Then
If Price2 > 0.0 Or Price2 = "" Then
If CType(Price2, Double) = Price2 Then
Result = True
End If
End If
End If
Return Result
End Function
-
Jul 19th, 2003, 12:00 AM
#6
Thread Starter
Junior Member
It doesnt' work either.. ... is there something wrong w/ my code?.. thanks
-
Jul 19th, 2003, 11:50 AM
#7
Sleep mode
What do you mean it doesn't work ?? I can't see anything wrong . I did test it and it returns false I think when 'Price2' is 0 or null .
-
Jul 20th, 2003, 12:14 AM
#8
Thread Starter
Junior Member
thanks
thanks for replying, Pirate.... First, please look at my code below and I'll explain to you why it didnt' work.....
-------------------------Form Class--------------------------------------------
Private Sub btTotal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btTotal.Click
Dim Order As New clsOrder()
.....
.....
If Not isValidPrice2(txtPrice2.Text) Then
Msg = "Price 2 is not valid." + vbCrLf
End If
If Msg <> "" Then
MsgBox(Msg, , "Invalid Data")
Else
txtTotal2.Text = Order.CalcTotal2(txtPrice2.Text, numQty2.Text)
End If
End Sub
Private Function isValidPrice2(ByVal Price2 As String) As Boolean
Dim Result As Boolean = False
If IsNumeric(Price2) Then
If Price2 > 0.0 Or Price2 = "" Then
If CType(Price2, Double) = Price2 Then
Result = True
End If
End If
End If
Return Result
End Function
-------------------clsOrder Class--------------------------------------------
Public Class clsOrder
Public Function CalcTotal2(ByVal Price2 As Double, ByVal Qty2 As Integer) As Double
Return Price2 * Qty2
End Function
End Class
---------------------------------------------------------------------------------
The way I set up is that I have a "Price2" textfield... I did the validation code above to have the textfield accept only the value that greater than 0... A "Price2 is not valid" messagebox will pop up if there is negative number in the "Price2" textfield...
Now when I tried your code above, it still gives me "Price2 is not valid" message box even if textfield is 0 or null... I know your code is right but somehow the message box still pop up....
Can you take a quick look at my clsOrder class or the way I arrange the code in the form class is right..... I know there's something wrong w/ the way I arrange the code but I couldnt' figure out the problem... Thanks a lot for helping...
-
Jul 20th, 2003, 02:44 AM
#9
can you not do something like this :
VB Code:
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
If e.KeyChar.IsNumber(e.KeyChar) Or e.KeyChar = "." Or e.KeyChar = Chr(Keys.Back) Or e.KeyChar = Chr(Keys.Delete) Then
e.Handled = False '/// only allow numerics , a . and the Back / Delete keys.
Else
e.Handled = True '/// dont allow any other keys.
End If
End Sub
Private Sub TextBox1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TextBox1.MouseUp
If Not IsNumeric(sender.text) Or sender.text = "." Then
sender.text = "" '/// incase stuff gets pasted in.
End If
End Sub
~
if a post is resolved, please mark it as [Resolved]
protected string get_Signature(){return Censored;}
[vbcode][php] please use code tags when posting any code [/php][/vbcode]
-
Jul 20th, 2003, 04:48 AM
#10
Thread Starter
Junior Member
thanks
yeah.. that's a good idea... thanks a lot..
thanks pirate and dynamic sysop for helping out....
-
Jul 20th, 2003, 11:43 AM
#11
yay gay
txtBox.Text.Length >= 0 in the beggining of the if maybe ?
\m/  \m/
-
Jul 20th, 2003, 12:58 PM
#12
Sleep mode
I've tried your code and it's working . It shows the msgbox "Price 2 is not..." when the value of assigned textbox is 0 , null or negative .
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|