Results 1 to 12 of 12

Thread: Textbox Validation question

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jul 2003
    Posts
    16

    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

  2. #2
    Addicted Member CoMMiE's Avatar
    Join Date
    Jul 2000
    Location
    Malaysia, Kuala Lumpur
    Posts
    179
    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

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jul 2003
    Posts
    16
    Thank you for replying... I tried that and still didnt' work... By the way, I am using Visual Basic.net

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Jul 2003
    Posts
    16

    Help

    Please advise what I need to do.. Thank You

  5. #5
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Try :
    VB Code:
    1. Private Function isValidPrice2(ByVal Price2 As String) As Boolean
    2.         Dim Result As Boolean = False
    3.         If IsNumeric(Price2) Then
    4.             If Price2 > 0.0 Or Price2 = "" Then
    5.                 If CType(Price2, Double) = Price2 Then
    6.                     Result = True
    7.                 End If
    8.             End If
    9.         End If
    10.         Return Result
    11.     End Function

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Jul 2003
    Posts
    16
    It doesnt' work either.. ... is there something wrong w/ my code?.. thanks

  7. #7
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    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 .

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Jul 2003
    Posts
    16

    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...

  9. #9
    Frenzied Member dynamic_sysop's Avatar
    Join Date
    Jun 2003
    Location
    Ashby, Leicestershire.
    Posts
    1,142
    can you not do something like this :
    VB Code:
    1. Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
    2.         If e.KeyChar.IsNumber(e.KeyChar) Or e.KeyChar = "." Or e.KeyChar = Chr(Keys.Back) Or e.KeyChar = Chr(Keys.Delete) Then
    3.             e.Handled = False '/// only allow numerics , a . and the Back / Delete keys.
    4.         Else
    5.             e.Handled = True '/// dont allow any other keys.
    6.         End If
    7.     End Sub
    8.  
    9.     Private Sub TextBox1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TextBox1.MouseUp
    10.         If Not IsNumeric(sender.text) Or sender.text = "." Then
    11.             sender.text = "" '/// incase stuff gets pasted in.
    12.         End If
    13.     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]

  10. #10

    Thread Starter
    Junior Member
    Join Date
    Jul 2003
    Posts
    16

    thanks

    yeah.. that's a good idea... thanks a lot..

    thanks pirate and dynamic sysop for helping out....

  11. #11
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    txtBox.Text.Length >= 0 in the beggining of the if maybe ?
    \m/\m/

  12. #12
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    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
  •  



Click Here to Expand Forum to Full Width