Click here to Skip to main content
15,880,796 members
Please Sign up or sign in to vote.
3.50/5 (2 votes)
I have a form that takes in a persons details which include contact number and email and saves it to a database. I have 2 validate functions (regex expressions) that ensure that the contact number and email are in the correct format, this works fine. I have a second form that displays the persons details, this also works fine. The problem is when I go to edit the persons details on the second form. The validate functions return false when the update button is clicked even though they should return true. They are the exact same functions in the second form as in the first form, in fact the 'If' statements calling the functions are identical. I have attached the code for the 'If' statement and functions. Can anyone help with this problem?

VB
If Not validatePhone(txtContactNumber.Text) Or Not validateEmail(txtEmail.Text) Then
.....
EndIf


VB
'Validate contact number.
    Public Function validatePhone(ByVal strContactNum As String) As Boolean 'validate contactNumber
        Dim numberPattern As String = "^\d{1,5}-\d{4,10}$" 'Regex expression pattern
        Dim rxPhone As New Regex(numberPattern) 'Regex Object
        Dim phoneValid As Boolean 'If true contact number is valid

        If Not String.IsNullOrEmpty(strContactNum) Then
            phoneValid = rxPhone.IsMatch(strContactNum) 'Check validity
        Else
            phoneValid = False 'Not valid contact number or txtContactNumber is empty
        End If
        Return phoneValid
    End Function

    'Validate email.
    Public Function validateEmail(ByVal strEmail As String) As Boolean 'validate email
        Dim emailPattern As String = "^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$" 'Regex expression pattern

        Dim rxEmail As New Regex(emailPattern) 'Regex Object
        Dim emailValid As Boolean 'If true email is valid

        If Not String.IsNullOrEmpty(strEmail) Then
            emailValid = rxEmail.IsMatch(strEmail) 'Check validity
        Else
            emailValid = False 'Not valid email or txtEmail is empty
        End If
        Return emailValid
    End Function


Just to be clear, the validate functions work fine in the first form but return false in the second form all the time, even when they should return true.

EDIT:

I solved this problem, it is because of white spaces at the end of each field coming back from the database. I have asked a new question to solve this problem here: http://www.codeproject.com/Questions/806935/Get-rid-of-white-spaces-at-end-of-field-from-datab
Posted
Updated 12-Aug-14 6:30am
v3
Comments
Jason Gleim 12-Aug-14 11:21am    
Have you set a breakpoint and stepped through the validation to see what is going on? I would suspect the values are not being passed in the way you think they are. My initial guess is that you might want to make the functions take the parameters ByRef instead of ByVal. This way they are working with the actual text objects and not copies.
Member 10804519 12-Aug-14 11:45am    
Hi @Jason Gleim, I have gone through debugging and it goes to the first part of the 'If' statement in the validate function, like it should, and returns false instead of true. I also tried your suggestion of changing ByVal to ByRef but that doesn't work either.
Sergey Alexandrovich Kryukov 12-Aug-14 11:56am    
It's a bad advice to use by ref in this case. Totally wrong.
And yes, use the debugger, but check all execution path and all conditions, not just first line. This is easy.
—SA
Member 10804519 12-Aug-14 12:28pm    
I solved this problem, it is because of white spaces at the end of each field coming back from the database. I have asked a new question to solve this problem here: http://www.codeproject.com/Questions/806935/Get-rid-of-white-spaces-at-end-of-field-from-datab

1 solution

You can also use the Trim function in VB or the RTRIM function in your query. Using varchar (as per your other question) is the right way to go for this data, but you can't always do this (e.g. it is someone else's schema).
 
Share this answer
 
v2
Comments
Member 10804519 13-Aug-14 5:21am    
What do you mean by 'someone else's schema'??? I am the one creating the project so it's me determining the schema.
PhilLenoir 13-Aug-14 10:01am    
That's exactly why I said "in this case"; as developers, sometimes we have to work with schemas we did not create and have no control over. I regularly have to query MS backs ends, that aren't always developed to my standards (e.g. Dynamics GP which uses a lot of char fields padded with spaces).

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900