开发实践教程1:试卷生成系统6.2 用户注册界面(FormRegist)

Posted VB.Net

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了开发实践教程1:试卷生成系统6.2 用户注册界面(FormRegist)相关的知识,希望对你有一定的参考价值。

版权声明:本文为博主原创文章,转载请在显著位置标明本文出处以及作者网名,未经作者允许不得用于商业目的。 

新用户需要通过此界面输入信息,提交注册申请。

窗体设计如下:

 图1-12

此窗口中需要注意:

1、当”账号名称“失去焦点时,验证该账号是否已经被使用,如果已经被使用,则显示错误标识并提示。

2、对输入的信息进行验证,特别是如果录入的身份证号码已经存在,给出错误提示。

3、输入并提交账号信息后,将数据保存到《用户表》,将对应”状态“设置为”申请“。

具体代码如下:

Imports System.Data.SqlClient

Public Class FormRegist

    Dim connection As SqlConnection

    Dim blNameRegist As Boolean

    Private Sub FormRegist_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        connection = New SqlConnection(databaseConnString)
        connection.Open()

        blNameRegist = False
    End Sub

    Private Sub btnSubmit_Click(sender As Object, e As EventArgs) Handles btnSubmit.Click
        Dim errMsg As String
        errMsg = checkData()
        If errMsg <> "" Then
            MessageBox.Show(errMsg, "注册错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End If

        If blNameRegist = True Then
            MessageBox.Show("账户名称已经被注册,请重新输入。", "注册错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End If

        Dim pass As String = clsMd5.toMD5(txtPass.Text)

        Dim command As New SqlCommand()
        command.Connection = connection
        command.CommandText = "insert into 用户表(账号,密码,申请时间,真实姓名,身份证号码,备注信息,状态) values(" &
                              "'" & txtName.Text & "','" & pass & "','" & Now.ToString("yyyy-MM-dd HH:mm:ss") & "'," &
                              "'" & txtTrueName.Text & "','" & txtNumber.Text & "','','申请')"

        command.ExecuteNonQuery()

        MessageBox.Show("注册申请已经提交,等待管理员审核。通常审核需要1-3天时间。")

        Me.Close()
    End Sub

    Private Function checkData() As String
        Dim specialChars As String = "',"",!,%,^,?, "
        Dim numberChars As String = "0123456789Xx"

        If txtName.Text = "" Then
            Return "账号名称不能为空"
        End If
        If txtName.Text = "admin" Then
            Return "账号名称已经被注册"
        End If
        For i As Integer = 0 To txtName.Text.Length - 1
            If specialChars.IndexOf(txtName.Text.Substring(i, 1)) > -1 Then
                Return "账号名称不能包含特殊字符"
            End If
        Next

        If txtTrueName.Text = "" Then
            Return "真实姓名不能为空"
        End If
        For i As Integer = 0 To txtTrueName.Text.Length - 1
            If specialChars.IndexOf(txtTrueName.Text.Substring(i, 1)) > -1 Then
                Return "真实姓名不能包含特殊字符"
            End If
        Next

        If txtNumber.Text = "" Then
            Return "身份证号不能为空"
        End If
        If txtNumber.Text.Length <> 18 Then
            Return "身份证号必须为18位"
        End If
        For i As Integer = 0 To txtNumber.Text.Length - 1
            If numberChars.IndexOf(txtNumber.Text.Substring(i, 1)) = -1 Then
                Return "不是有效的身份证号"
            End If
        Next
        Dim command As New SqlCommand()
        command.Connection = connection
        command.CommandText = "select top 1 账号 from 用户表 where (身份证号码='" & txtNumber.Text & "') and (状态='启用')"
        Dim rdUser As SqlDataReader
        rdUser = command.ExecuteReader(CommandBehavior.SingleRow)
        If rdUser.HasRows Then
            rdUser.Close()
            Return "身份证号已经被注册,请重新输入。"
        End If
        rdUser.Close()

        If txtPass.Text = "" Then
            Return "用户密码不能为空"
        End If
        If txtPass.Text.Length < 6 Then
            Return "用户密码必须6位以上"
        End If
        For i As Integer = 0 To txtPass.Text.Length - 1
            If specialChars.IndexOf(txtPass.Text.Substring(i, 1)) > -1 Then
                Return "用户密码不能包含特殊字符"
            End If
        Next

        If txtPass.Text <> txtPassRepeat.Text Then
            Return "密码不能为空"
        End If

        Return ""
    End Function


    Private Sub txtName_GotFocus(sender As Object, e As EventArgs) Handles txtName.GotFocus
        pbInfo.Visible = False
    End Sub

    Private Sub txtName_LostFocus(sender As Object, e As EventArgs) Handles txtName.LostFocus
        Dim specialChars As String = "',"",!,%,^,?, "

        Dim userName As String
        userName = txtName.Text.Trim
        If userName = "" Then
            Exit Sub
        End If
        For i As Integer = 0 To userName.Length - 1
            If specialChars.IndexOf(userName.Substring(i, 1)) > -1 Then
                MessageBox.Show("账号名称不能包含特殊字符", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error)
                Exit Sub
            End If
        Next


        Dim command As New SqlCommand()

        command.Connection = connection
        command.CommandText = "select top 1 账号 from 用户表 where (账号='" & userName & "')"
        Dim rdUser As SqlDataReader
        rdUser = command.ExecuteReader(CommandBehavior.SingleRow)

        If rdUser.HasRows Then
            blNameRegist = True
            pbInfo.Image = pbWrong.Image
            ttInfo.SetToolTip(pbInfo, "账户名称已经被注册,请重新输入。")
        Else
            blNameRegist = False
            pbInfo.Image = pbRight.Image
            ttInfo.SetToolTip(pbInfo, "账户名称尚未被注册。")
        End If
        pbInfo.Visible = True
        rdUser.Close()
    End Sub


End Class

由于.net平台下C#和vb.NET很相似,本文也可以为C#爱好者提供的参考。

学习更多vb.net知识,请参看 vb.net 教程 目录

以上是关于开发实践教程1:试卷生成系统6.2 用户注册界面(FormRegist)的主要内容,如果未能解决你的问题,请参考以下文章

开发实践教程1:试卷生成系统6 窗体设计总览

开发实践教程1:试卷生成系统6.1 登录界面(FormLogin)

开发实践教程1:试卷生成系统6.12 试卷管理(FormTestPaperManager)

开发实践教程1:试卷生成系统3 系统设计

开发实践教程1:试卷生成系统6.18用户查询(FormUserQuery)

开发实践教程1:试卷生成系统6.19用户备注(FormUserMemo)