vb.net winform项目中验证函数的问题。 [英] Problem with validate functions in vb.net winform project.

查看:71
本文介绍了vb.net winform项目中验证函数的问题。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表单,其中包含人员详细信息,其中包括联系电话和电子邮件,并将其保存到数据库中。我有2个验证函数(正则表达式),确保联系号码和电子邮件格式正确,这很好。我有第二个表单显示人员的详细信息,这也很好。问题是当我去编辑第二个表格上的人员细节时。单击更新按钮时,验证函数返回false,即使它们应返回true。它们与第一种形式中的第二种形式完全相同,实际上调用函数的If语句是相同的。我附上了'If'语句和函数的代码。任何人都可以帮忙解决这个问题吗?



 如果  validatePhone(txtContactNumber.Text)  validateEmail(txtEmail) .Text)然后 
.....
EndIf





 ' 验证联系电话。 
公共 功能 validatePhone( ByVal strContactNum 作为 字符串作为 布尔 ' 验证contactNumber
Dim numberPattern < span class =code-keyword> As String = ^ \d {1,5} -\d {4,10} $ ' 正则表达式表达式
Dim rxPhone 作为 正则表达式(numberPattern)' 正则表达式对象
Dim phoneValid As Boolean 如果真实的联系电话号码有效

如果 字符串 .IsNullOrEmpty(strContactNum)然后
phoneValid = rxPhone.IsMatch(st rContactNum)' 检查有效期
其他
phoneValid = False ' 无效联系电话或txtContactNumber为空
结束 如果
返回 phoneValid
结束 功能

' 验证电子邮件。
公开 功能 validateEmail( ByVal strEmail 正如 字符串作为 布尔 < span class =code-comment>' 验证电子邮件
Dim emailPattern 作为 字符串 = ^ [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] $ 正则表达式表达式

Dim rxEmail 作为 正则表达式(emailPattern)' 正则表达式对象
Dim emailValid As 布尔 ' 如果真正的电子邮件有效

如果 字符串 .IsNullOrEmpty(strEmail) 然后
emailValid = rxEmail.IsMatch(strEmail)' 检查有效期
否则
emailValid = 错误 ' 无效的电子邮件或txtEmail为空
结束 如果
返回 emailValid
结束 功能





为了清楚起见,验证函数在第一种形式中工作正常,但在第二种形式中一直返回false,即使它们应该返回true。



编辑:



我解决了这个问题,这是因为每个字段末尾的空格都是从数据库返回的。我在这里问了一个新问题来解决这个问题:http://www.codeproject.com/Questions/806935/Get-rid-of-white-spaces-at-end-of-field-from-datab

解决方案

' 正则表达式
Dim rxPhone 作为 正则表达式(numberPattern)' 正则表达式对象
昏暗 phoneValid 作为 布尔 ' 如果真实的联系电话号码有效

如果 字符串 .IsNullOrEmpty(strContactNum)然后
phoneValid = rxPhone.IsMatch(strContactNum)' 检查有效期
否则
phoneValid = 错误 ' 无效的联系电话或txtContactNumber为空
结束 如果
返回 phoneValid
结束 功能

' 验证电子邮件。
公共 功能 validateEmail( ByVal strEmail 作为 字符串作为 布尔 ' 验证电子邮件
Dim emailPattern 作为 字符串 = ^ [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]


' 正则表达式表达式

Dim rxEmail As 正则表达式(emailPattern)' 正则表达式对象
Dim emailValid 作为 布尔 ' 如果真实电子邮件有效

如果 字符串 .IsNullOrEmpty(strEmail)然后
emailValid = rxEmail.IsMatch( strEmail)' 检查有效期
其他
emailValid = 错误 ' 无效电子邮件或txtEmail为空
结束 如果
返回 emailValid
结束 功能





为了清楚起见,验证函数在第一种形式下工作正常但在第二种形式中返回false所有时间,即使它们应该返回真实。



编辑:



我解决了这个问题,它是因为每个字段末尾的空格从数据库返回。我在这里提出了一个新问题来解决这个问题:http://www.codeproject.com/Questions/806935/Get-rid-of-white-spaces-at-end-of-field-from-datab


您也可以在VB中使用Trim函数或在查询中使用RTRIM函数。使用varchar(根据您的其他问题)是获取此数据的正确方法,但您不能总是这样做(例如,它是其他人的架构)。


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?

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



'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

解决方案

" '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


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


这篇关于vb.net winform项目中验证函数的问题。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆