datagridview中的电子邮件地址验证问题 [英] Email Address Validation in datagridview Problem

查看:127
本文介绍了datagridview中的电子邮件地址验证问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我的电子邮件地址验证码存在问题.当我单击该特定单元格(电子邮件地址"列)时,
无论我是否要输入该单元格,都不允许我移至下一个单元格.....我将代码粘贴在这里!!请尽快帮助.. !!!
在此先感谢

Hi, I am facing a problem in my email address validation codes. when i click on that particular cell (email Address Column),
whether or not i want to enter in that cell it does not allow me to move to the next cell..... i am pasting my codes here.!! Please help sooon..!!!
Thanks in Advance

Private Sub DataGridView2_CellValidating(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellValidatingEventArgs) Handles dgCompany.CellValidating
        If e.ColumnIndex = 6 Then
            If Not IsEmailValid(e.FormattedValue) Then
                MsgBox("Enter Valid Email Address")
                e.Cancel = True
                Dim result As DialogResult = MessageBox.Show("The Email Address is not valid." & " Do you want re-enter it?", "Invalid Email Address", MessageBoxButtons.YesNo, MessageBoxIcon.Error)
                If result = Windows.Forms.DialogResult.Yes Then
                    e.Cancel = True
                End If
            End If
        ElseIf dgCompany.CurrentRow.Cells(6).Value.ToString() = String.Empty Then
            e.Cancel = False
        End If
    End Sub
    Function IsEmailValid(ByVal EmailAddress As String) As [Boolean]
        Dim pattern 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]$"
        Dim emailAddressMatch As Match = Regex.Match(EmailAddress, pattern)
        If emailAddressMatch.Success Then
            IsEmailValid = True
        Else
            IsEmailValid = False
        End If
    End Function

推荐答案

昏暗的emailAddressMatch作为匹配项= Regex.Match(EmailAddress,pattern) 如果emailAddressMatch.Success然后 IsEmailValid = True 别的 IsEmailValid = False 万一 结束函数
" Dim emailAddressMatch As Match = Regex.Match(EmailAddress, pattern) If emailAddressMatch.Success Then IsEmailValid = True Else IsEmailValid = False End If End Function


如果要直接在datagridview中接受输入,则字符串类型的列将接受任何输入,但是在进行无效输入时会显示DataGridView默认错误对话框,即整数类型列为123a要求处理datagridview的DataError事件.
因此,您可以使用DataGridView的CellValidating事件来验证整数类型列以及其他类型.

步骤:
使用DataGridView的CellValidating事件
获取为其调用事件的单元格
如果单元格处于EditMode,则
执行验证

CellValidating事件:
If you want take input directly in datagridview, column of string type takes any input but DataGridView default error dialog is shown when making invalid input i-e 123a for integer type column that asks to handle the DataError event of datagridview .
So you can use CellValidating event of DataGridView to validate integer type column as well as other.

Steps:
Use DataGridView''s CellValidating event
Get the cell for which the event is called
If the cell is in EditMode then
Perform validation

CellValidating Event:
Private Sub DataGridView1_CellValidating(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellValidatingEventArgs) Handles DataGridView1.CellValidating
Dim cell As DataGridViewCell = DataGridView1.Item(e.ColumnIndex, e.RowIndex)

If cell.IsInEditMode Then
Dim c As Control = DataGridView1.EditingControl

Select Case DataGridView1.Columns(e.ColumnIndex).Name
Case "sessno", "rno"
c.Text = CleanInputNumber(c.Text)
Case "name"
c.Text = CleanInputAlphabet(c.Text)
End Select
End If
End Sub


Utility Functions: Private Function CleanInputAlphabet(ByVal str As String) As String
Return System.Text.RegularExpressions.Regex.Replace(str, "[0-9\b\s-]", "")
End Function 

Private Function CleanInputNumber(ByVal str As String) As String
Return System.Text.RegularExpressions.Regex.Replace(str, "[a-zA-Z\b\s-.]", "")
End Function


这篇关于datagridview中的电子邮件地址验证问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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