11 Replies - 1427 Views - Last Post: 09 June 2014 - 03:08 AM Rate Topic: -----

#1 Thinus du Pisanie  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 107
  • Joined: 07-October 11

Error Message on invalid characters

Posted 03 June 2014 - 01:39 AM

Good day

I have a question about error message if someone maybe can assist me.

I have a function.

  Private Function Field_Err_Track() As Boolean
        strErr = "Please "
        Field_Err_Track = False
        If txtTrack.Text() = "" Then
            Field_Err_Track = True
            strErr = strErr & "Enter the following: * Tracking Source"
        End If

        If txtTrack.Text.Contains("?") Then
            Field_Err_Track = True
            strErr = strErr & " Check for invalit characters like (?)"
        End If

        If txtTrack.Text.Contains("@") Then
            Field_Err_Track = True
            strErr = strErr & " Check for invalit characters like (@)"
        End If

        If txtTrack.Text.Contains("#") Then
            Field_Err_Track = True
            strErr = strErr & " Check for invalit characters like (#)"
        End If

        If txtTrack.Text.Contains("$") Then
            Field_Err_Track = True
            strErr = strErr & " Check for invalit characters like ($)"
        End If

        If txtTrack.Text.Contains("%") Then
            Field_Err_Track = True
            strErr = strErr & " Check for invalit characters like (%)"
        End If

        If txtTrack.Text.Contains("^") Then
            Field_Err_Track = True
            strErr = strErr & " Check for invalit characters like (^)"
        End If

        If txtTrack.Text.Contains("*") Then
            Field_Err_Track = True
            strErr = strErr & " Check for invalit characters like (*)"
        End If

        If txtTrack.Text.Contains("+") Then
            Field_Err_Track = True
            strErr = strErr & " Check for invalit characters like (+)"
        End If

        If txtTrack.Text.Contains("/") Then
            Field_Err_Track = True
            strErr = strErr & " Check for invalit characters like (/)"
        End If

        If txtTrack.Text.Contains("\") Then
            Field_Err_Track = True
            strErr = strErr & " Check for invalit characters like (\)"
        End If

        Dim s As String = "'"
        If txtTrack.Text.Contains(s) Then
            Field_Err_Track = True
            strErr = strErr & " Check for invalit characters like (')"
        End If

    End Function


Then on my button click event

 Protected Sub cndAddTrack_Click(sender As Object, e As EventArgs) Handles cndAddTrack.Click

        If Field_Err_Track() Then
            Set_Alert(strErr)
        Else
            If Key_Exist_Track() Then
                strErr = "Tracking Source already exists for this KPA"
                Set_Alert(strErr)
            Else
                Update_Track()
            End If
        End If
        gvTrack.DataBind()
    End Sub


Now if there is a invalid character in the text box like $ or % or # the error message pops that states it is a invalid character in the text box.

But as soon as a ' occurs like in the text ("The boy's friend") the error message don't pop that states the ' is a invalid character message.

If someone maybe can assist I will appreciate.

Is This A Good Question/Topic? 0
  • +

Replies To: Error Message on invalid characters

#2 astonecipher  Icon User is offline

  • D.I.C.
  • member icon

Reputation: 795
  • View blog
  • Posts: 3,486
  • Joined: 03-December 12

Re: Error Message on invalid characters

Posted 03 June 2014 - 04:08 AM

Try escaping it,

"\'"

The other option, would be to use a regular expression to find all of the characters you won't allow.
Was This Post Helpful? 1
  • +
  • -

#3 Thinus du Pisanie  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 107
  • Joined: 07-October 11

Re: Error Message on invalid characters

Posted 03 June 2014 - 04:11 AM

Ok so if I use this

 	      If txtTrack.Text.Contains("\'") Then
	          Field_Err_Track = True
	          strErr = strErr & " Check for invalid characters like (')"
	      End If


The Message will then pop if there is a ' in the text box do I understand this correctly.

This post has been edited by Thinus du Pisanie: 03 June 2014 - 04:11 AM

Was This Post Helpful? 0
  • +
  • -

#4 astonecipher  Icon User is offline

  • D.I.C.
  • member icon

Reputation: 795
  • View blog
  • Posts: 3,486
  • Joined: 03-December 12

Re: Error Message on invalid characters

Posted 03 June 2014 - 04:21 AM

Never mind,

Use "'" (double quote, single, double)
Your attempt looks for a space a single quote followed by a space. The escape character I gave does not work for vb because it uses it for comments.
Was This Post Helpful? 0
  • +
  • -

#5 Thinus du Pisanie  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 107
  • Joined: 07-October 11

Re: Error Message on invalid characters

Posted 03 June 2014 - 04:26 AM

Like this

If txtTrack.Text.Contains("'") Then
	    Field_Err_Track = True
	    strErr = strErr & " Check for invalid characters like (')"
	End If


I I use it like it is showing on the above code this is error I get.

An exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll but was not handled in user code

Additional information: Incorrect syntax near 's'.

Unclosed quotation mark after the character string ' '.

Was This Post Helpful? 0
  • +
  • -

#6 astonecipher  Icon User is offline

  • D.I.C.
  • member icon

Reputation: 795
  • View blog
  • Posts: 3,486
  • Joined: 03-December 12

Re: Error Message on invalid characters

Posted 03 June 2014 - 04:31 AM

Is the for a sql statement?

If so, this is more along the lines of what you want,

Prepared statements
Was This Post Helpful? 0
  • +
  • -

#7 Thinus du Pisanie  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 107
  • Joined: 07-October 11

Re: Error Message on invalid characters

Posted 03 June 2014 - 04:34 AM

No here is the function

Private Function Field_Err_Track() As Boolean

If txtTrack.Text.Contains("'") Then
	          Field_Err_Track = True
	          strErr = strErr & " Check for invalit characters like (')"
	      End If
	 
	  End Function


Here is the button click event

  Protected Sub cmdUpdate_Click(ByVal sender As Object, ByVal e As EventArgs) Handles cmdUpdate.Click

        If Field_Err() Then
            Set_Alert(strErr)
        Else
            Update_KPA()
            lblPerf_KPA_ID.Text = "0"
            Set_KPA()
            Check_KPA_Weight()
        End If

    End Sub


So basically if the button is clicked and the text box = a character like ' then the message box with the error message (Check for invalid characters like (')) must pop and the rest of the statement must be terminated
Was This Post Helpful? 0
  • +
  • -

#8 astonecipher  Icon User is offline

  • D.I.C.
  • member icon

Reputation: 795
  • View blog
  • Posts: 3,486
  • Joined: 03-December 12

Re: Error Message on invalid characters

Posted 03 June 2014 - 04:38 AM

What is "s"? Based on your error it looks like a sql issue. If an error is found does the set_error return to the textbox showing the issue?
Was This Post Helpful? 0
  • +
  • -

#9 Thinus du Pisanie  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 107
  • Joined: 07-October 11

Re: Error Message on invalid characters

Posted 03 June 2014 - 04:47 AM

Ok if I do this

 If txtWeight.Text.Contains(".") Then
            Field_Err = True
            strErr = strErr & " Check for invalit characters like (.)"
        End If


Here is my button click event again.

 Protected Sub cmdUpdate_Click(ByVal sender As Object, ByVal e As EventArgs) Handles cmdUpdate.Click

        If Field_Err() Then
            Set_Alert(strErr)
        Else
            Update_KPA()
            lblPerf_KPA_ID.Text = "0"
            Set_KPA()
            Check_KPA_Weight()
        End If

    End Sub


So if there is a . present in the textbox in any sentence I get a popup error stating Check for invalit characters like (.)

But if I use this

   If txtWeight.Text.Contains("'") Then
            Field_Err = True
            strErr = strErr & " Check for invalit characters like (')"
        End If


For some reason if there is a ' present in the textbox the error does not pop and I don't know why. What I want to know is why does the error not pop reading that there is a ' present in the textbox
Was This Post Helpful? 0
  • +
  • -

#10 astonecipher  Icon User is offline

  • D.I.C.
  • member icon

Reputation: 795
  • View blog
  • Posts: 3,486
  • Joined: 03-December 12

Re: Error Message on invalid characters

Posted 03 June 2014 - 04:48 AM

Your function does not return anything either. Could that be causing the rest of the program to run even when an error is found?
Was This Post Helpful? 0
  • +
  • -

#11 Thinus du Pisanie  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 107
  • Joined: 07-October 11

Re: Error Message on invalid characters

Posted 03 June 2014 - 04:53 AM

No the function pops my error message and kicks me out of the if statement if there is a . present but if I run the same function altering the values to pop the error message using a ' the function runs but for some reason does not pick up that there is a ' in the textbox.

I just want a way any way to ensure if there is a ' in the textbox that a error message will pop and say invalid character '.
Was This Post Helpful? 0
  • +
  • -

#12 Thinus du Pisanie  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 107
  • Joined: 07-October 11

Re: Error Message on invalid characters

Posted 09 June 2014 - 03:08 AM

Good day

Ok I figured it out you where right about escaping it.

It was just a matter of where.

   If txtTrack.Text.Contains("'") Then
            Field_Err_Track = True
            strErr = strErr & " Check for invalid characters like (\')"
        End If


The escape must be here

strErr = strErr & " Check for invalid characters like (\')"


And not in the

If txtTrack.Text.Contains("'")


But thanks very much appreciate.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1