Results 1 to 11 of 11

Thread: [RESOLVED] Text validation help

  1. #1

    Thread Starter
    Addicted Member _ivo_'s Avatar
    Join Date
    Sep 2009
    Location
    Zagreb
    Posts
    140

    Resolved [RESOLVED] Text validation help

    Visual Web Developer 2010 Express
    This is an ASP.net project

    I have searched but wasn't successful

    I have two text boxes and I need to make small calculator.
    I have made one "IF" for checking is there something in textboxes, if they are empty then I get message that I didn't insert anything.

    And if I insert numbers then I get the result.
    This is ok.

    I have a problem with validation

    I need ONLY numbers to be inserted, if I insert something that is not integer I need a message that this is not a number.

    Please, hlp.

    This is my code :

    Code:
    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
    
            If TextBox1.Text = ("") Or TextBox2.Text = ("") Then
                Label5.Text = ("Niste unjeli dva broja, pokusajte ponovo")
            Else
                Dim prvi As Integer = TextBox1.Text
                Dim drugi As Integer = TextBox2.Text
                Label5.Text = (prvi + drugi)
            End If
    
        End Sub

  2. #2
    Frenzied Member avrail's Avatar
    Join Date
    Mar 2006
    Location
    Egypt, Cairo
    Posts
    1,221

    Re: Text validation help

    hay,
    you can use RegularExpressions to do such thing
    Code:
    Dim numericReg As New Regex("^[\d]*$")
    If numericReg.IsMatch("12345") Then
            // the match done 
    	Return True
    Else
    //  there is no match 
    /// add the message that you want 
    	Return False
    End If
    You Don't Have to Rate Me.
    I'm Not a Civilized Man I'm the Civilization it self
    White or Black, Living or Dieing and 0 or 1 that's MY life
    iam an Object in Object Oriented Life
    my blog : http://refateid.blogspot.com/
    twitter :@avrail
    010011000111010101110110001000000100110101111001001000000101000001100011

  3. #3

    Thread Starter
    Addicted Member _ivo_'s Avatar
    Join Date
    Sep 2009
    Location
    Zagreb
    Posts
    140

    Re: Text validation help

    Hm... Really sorry but I don't know how to implement this in my code, I didn't use this till now

  4. #4
    Frenzied Member avrail's Avatar
    Join Date
    Mar 2006
    Location
    Egypt, Cairo
    Posts
    1,221

    Re: Text validation help

    ok,

    Code:
    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
    
            If TextBox1.Text = ("") Or TextBox2.Text = ("") Then
                Label5.Text = ("Niste unjeli dva broja, pokusajte ponovo")
            Else
                Dim numericReg As New System.Text.RegularExpressions.Regex("^[\d]*$")
                 If numericReg.IsMatch(TextBox1.Text) AndAlso numericReg.IsMatch(TextBox2.Text) Then
                   Dim prvi As Integer = TextBox1.Text
                   Dim drugi As Integer = TextBox2.Text
                   Label5.Text = (prvi + drugi)
                  Else
                  Label5.Text = "please enter numbers"
                 End If
                
            End If
    
        End Sub
    i think this will make it for you
    You Don't Have to Rate Me.
    I'm Not a Civilized Man I'm the Civilization it self
    White or Black, Living or Dieing and 0 or 1 that's MY life
    iam an Object in Object Oriented Life
    my blog : http://refateid.blogspot.com/
    twitter :@avrail
    010011000111010101110110001000000100110101111001001000000101000001100011

  5. #5
    Frenzied Member avrail's Avatar
    Join Date
    Mar 2006
    Location
    Egypt, Cairo
    Posts
    1,221

    Re: Text validation help

    but really i don't know if this was right or not

    Code:
    Dim drugi As Integer = TextBox2.Text
    if it was right then the code will work fine.
    You Don't Have to Rate Me.
    I'm Not a Civilized Man I'm the Civilization it self
    White or Black, Living or Dieing and 0 or 1 that's MY life
    iam an Object in Object Oriented Life
    my blog : http://refateid.blogspot.com/
    twitter :@avrail
    010011000111010101110110001000000100110101111001001000000101000001100011

  6. #6

    Thread Starter
    Addicted Member _ivo_'s Avatar
    Join Date
    Sep 2009
    Location
    Zagreb
    Posts
    140

    Re: Text validation help

    Thanks so much, this works great and now I have an example, thanks!

  7. #7
    Frenzied Member avrail's Avatar
    Join Date
    Mar 2006
    Location
    Egypt, Cairo
    Posts
    1,221

    Re: [RESOLVED] Text validation help

    glad to hear this,
    if you want to learn more about RegularExpressions

    http://gskinner.com/RegExr/
    You Don't Have to Rate Me.
    I'm Not a Civilized Man I'm the Civilization it self
    White or Black, Living or Dieing and 0 or 1 that's MY life
    iam an Object in Object Oriented Life
    my blog : http://refateid.blogspot.com/
    twitter :@avrail
    010011000111010101110110001000000100110101111001001000000101000001100011

  8. #8

    Thread Starter
    Addicted Member _ivo_'s Avatar
    Join Date
    Sep 2009
    Location
    Zagreb
    Posts
    140

    Re: Text validation help

    Quote Originally Posted by avrail View Post
    but really i don't know if this was right or not

    Code:
    Dim drugi As Integer = TextBox2.Text
    if it was right then the code will work fine.
    yes, it is correct because I need two numbers

    thanks for the link!

  9. #9
    Frenzied Member avrail's Avatar
    Join Date
    Mar 2006
    Location
    Egypt, Cairo
    Posts
    1,221

    Re: [RESOLVED] Text validation help

    ok, great i was just want to check if it was right or wrong
    you can check if the numbers was 2 digits or not
    use this
    Code:
    System.Text.RegularExpressions.Regex("^[\d]{2}$")
    You Don't Have to Rate Me.
    I'm Not a Civilized Man I'm the Civilization it self
    White or Black, Living or Dieing and 0 or 1 that's MY life
    iam an Object in Object Oriented Life
    my blog : http://refateid.blogspot.com/
    twitter :@avrail
    010011000111010101110110001000000100110101111001001000000101000001100011

  10. #10

    Thread Starter
    Addicted Member _ivo_'s Avatar
    Join Date
    Sep 2009
    Location
    Zagreb
    Posts
    140

    Re: [RESOLVED] Text validation help

    Quote Originally Posted by avrail View Post
    ok, great i was just want to check if it was right or wrong
    you can check if the numbers was 2 digits or not
    use this
    Code:
    System.Text.RegularExpressions.Regex("^[\d]{2}$")
    thanks

  11. #11
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: [RESOLVED] Text validation help

    Hey,

    You might want to think about using the built in controls for this, i.e:

    http://msdn.microsoft.com/en-us/library/bwd43d0x.aspx

    That way you will get validation both on the client side, and the server side.

    Gary

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