vb.net - Email validation issue
问题描述
I am trying to validate email address field. I did it using Regex
and it works fine but the issue is
I have set e.cancel
to True
in validating
event, due to which it doesn't allow user to change focus unless user enters a correct email-id, even this is not the problem but it wont even allow the user to close the window/form.
I mean if the user is trying to abort
the complete transaction
what is the need of him/her to enter valid email id.
Here is my code -
Private Sub tbemail_Validating(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles tbemail.Validating
Dim pattern As String = "^[a-z][a-z|0-9|]*([_][a-z|0-9]+)*([.][a-z|0-9]+([_][a-z|0-9]+)*)?@[a-z][a-z|0-9|]*\.([a-z][a-z|0-9]*(\.[a-z][a-z|0-9]*)?)$"
Dim match As System.Text.RegularExpressions.Match = Regex.Match(tbemail.Text.Trim(), pattern, RegexOptions.IgnoreCase)
If (match.Success) Then
Else
MessageBox.Show("Please enter a valid email id", "Checking")
e.Cancel = True
End If
End Sub
解决方案
您应该将Cancel按钮的CausesValidation
属性设置为。这样,单击它不会引发事件,即使最后一个控件中的数据无效,您也可以关闭表单。这假定表单是通过调用显示的。False
Validating
ShowDialog
推荐阅读
- python - 创建一个函数,我可以在其中将名字和姓氏输入字典
- html - Div 没有拉伸到父级的 100% 高度
- sql - 如何在活动的 android 中定义新列并使用 setter、getter?
- javascript - 如何在反应js javascript中使用for循环动态更新html内容
- mysql - ERROR tool.BaseSqoopTool: Error parsing arguments for import: 将 Mysql 数据导入 S3 时
- android - 有没有办法以编程方式对 Android 上的系统权限对话框执行输入操作?
- swift - sum.swift:29:19:错误:在范围内找不到“CFAbsoluteTimeGetCurrent”
- python - 删除链表元素
- excel - 溢出范围内的过滤范围
- python - 加载具有一列的 csv 文件,其中包含一个以字符串形式写入的 numpy 数组