使用visual basic在文本框中验证

时间:2015-05-09 14:50:50

标签: asp.net vb.net validation

我即将使用vb对文本框进行验证。有人可以帮助我验证我是否使用vb语言将文本留空? N如何验证角色,电话号码和电子邮件?

    Imports System.Data
Imports System.Data.SqlClient
Imports System.IO
Imports System.Web.UI.WebControls


Partial Class _Default
    Inherits System.Web.UI.Page
    'Dim con As New SqlConnection(ConfigurationManager.ConnectionStrings("dbconnection").ConnectionString)`


Dim con As New SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\user\Documents\Visual Studio 2010\feedbackScholarInn\App_Data\Database.mdf;Integrated Security=True;User Instance=True")

        Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim date1 As Date = Date.Now
            con.Open()
            Dim cmd As New SqlCommand("insert into complainer(fullname,no_Tel,email) values(@fullname,@no_Tel,@email)", con)
            cmd.Parameters.AddWithValue("@fullname", txtFullname.Text)
            cmd.Parameters.AddWithValue("@no_Tel", txtno_Tel.Text)
            cmd.Parameters.AddWithValue("@email", txtEmail.Text)
            cmd.ExecuteNonQuery()
            con.Close()

            con.Open()
            Dim cmd2 As New SqlCommand("insert into feedbackDetails(message,subject,status,typeofFeedback,date,roomNo) values(@message,@subject,@status,@typeofFeedback,@date,@roomNo)", con)
            cmd2.Parameters.AddWithValue("@message", txtMessage.Text)
            cmd2.Parameters.AddWithValue("@status", "New")
            cmd2.Parameters.AddWithValue("@subject", txtSubject.Text)
            cmd2.Parameters.AddWithValue("@typeofFeedback", dropDownFeedback.SelectedValue)
            cmd2.Parameters.AddWithValue("@date", date1)
            cmd2.Parameters.AddWithValue("@roomNo", txtRoom.Text)
            cmd2.ExecuteNonQuery()


            con.Close()


        End Sub
    End Class

1 个答案:

答案 0 :(得分:0)

对于空文本,您可以使用Leave Event,但我更喜欢TextBox上的KeyUp Event:

 Private Sub txtFullname_Leave(sender As Object, e As KeyEventArgs) Handles txtFullname.Leave
    If txtFullname.Text.Length < 1 Then
        MessageBox("Please enter a text !") 
    End If
 End Sub

 Private Sub txtEmail_Leave(sender As Object, e As KeyEventArgs) Handles txtEmail.Leave
    IsEmail(txtEmail.text)
 End Sub

'手机无验证请阅读:https://stackoverflow.com/a/24895684/2269262
'对于电话验证,按国家/地区格式进行差异验证  “你也可以读到这个:https://msdn.microsoft.com/en-us/library/system.net.mail.mailaddress.aspx

Function IsEmail(Byval email as string) as boolean
     Static emailExpression As New Regex("^[_a-z0-9-]+(.[a-z0-9-]+)@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,4})$")
     return emailExpression.IsMatch(email)
End Function