如何检查电子邮件ID中的值是否正确?

时间:2017-08-23 13:51:17

标签: c# sql asp.net

当我在Textbox中输入值并单击提交按钮时,输入的值应传递到数据库并检查输入的文本值是否有效..

如果输入的值正确,则应显示下一个div ..

<div class="modal fade" id="myModal" role="dialog">
        <div class="modal-dialog modal-sm">
            <div class="modal-content" style="width: 500px; margin-left: -24%;">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal">&times;</button>
                    <h4 class="modal-title" style="text-align:center;">Upload image(Max 5Mb)</h4>
                </div>
                <div class="modal-body">
                    <asp:Label ID="lblMsg" runat="server"></asp:Label>
                    <div id="showimage" runat="server">
                        <asp:TextBox ID="txtHosEmail" runat="server" class="form-control" placeholder="Enter Your Email" Style="border: none;"></asp:TextBox>
                        <div class="modal-footer">
                            <%--<button type="button" class="btn btn-sucess" data-dismiss="modal" style="background-color: #79a35a;">Start Upload</button>--%>
                            <asp:Button ID="btnCheck" runat="server" class="btn btn-default" OnClick="btnCheck_Click" Text="Submit" Style="background-color: #79a35a;" />
                            <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                        </div>
                    </div>
                    <div id="hideimage" style="display: none;" runat="server">
                        <span>Image Name</span>
                        <asp:TextBox ID="txtimage" runat="server" placeholder="Enter Your Image Name" class="form-control"></asp:TextBox>
                        <asp:FileUpload ID="fileimage" runat="server" class="form-control inline v-middle input-s" />
                        <div class="modal-footer">
                            <%--<button type="button" class="btn btn-sucess" data-dismiss="modal" style="background-color: #79a35a;">Start Upload</button>--%>
                            <asp:Button ID="btnupload" runat="server" OnClick="btnupload_Click" class="btn btn-default" Text="Start Upload" Style="background-color: #79a35a;" />
                            <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                        </div>
                    </div>
                </div>

            </div>
        </div>
    </div>

Onclick "btnCheck_Click"值正确,应显示下一个hideimage Div ..

protected void btnCheck_Click(object sender, EventArgs e)
    {
        if (!CheckLogin(txtHosEmail.Text.ToString().Trim()))
        {
            showimage.Visible = false;
            hideimage.Visible = true;
        }
        else
        {
            hideimage.Visible = true;
            lblMsg.Text = "This Email is InCorrect";
        }
    }

    protected bool CheckLogin(string Email)
    {
        SqlConnection con = new SqlConnection(constr);
        SqlCommand cmd = new SqlCommand("select Email from PadappaiCheckEmail where Email = '" + Email + "'", con);
        cmd.Parameters.Add("@Email", SqlDbType.VarChar).Value = Email;
        string id = "";
        try
        {
            con.Open();
            id = cmd.ExecuteScalar() == null ? "" : cmd.ExecuteScalar().ToString();
        }
        catch (Exception ex)
        {
            //...
        }
        finally
        {
            con.Close();
        }
        if (String.IsNullOrEmpty(id)) return false;
        return true;
    }

3 个答案:

答案 0 :(得分:3)

您的if阻止方向错误。

if (!CheckLogin(txtHosEmail.Text.ToString().Trim()))
{
    showimage.Visible = false;
    hideimage.Visible = true;
}
else
{
    hideimage.Visible = true;
    lblMsg.Text = "This Email is InCorrect";
}

应该是:

if (CheckLogin(txtHosEmail.Text.Trim()))
{
    showimage.Visible = false;
    hideimage.Visible = true;
}
else
{
    hideimage.Visible = true;
    lblMsg.Text = "This Email is InCorrect";
}

请注意删除!(以及删除不必要的ToString()电话)。 如果找到电子邮件地址, CheckLogin会返回true

答案 1 :(得分:2)

您可以在代码中改进许多内容。

您正在调用RoomManager.reasonInit()方法两次

ExecuteScalar

你应该只召唤一次

id = cmd.ExecuteScalar() == null ? "" : cmd.ExecuteScalar().ToString();

然后你应该使用你的SQL参数

var result = cmd.ExecuteScalar();
d = result.ToString() ?? "";

最后,你不应该沉默你的例外,你应该删除空的捕获。

答案 2 :(得分:-3)

使用HTML5,您可以设置输入类型=“电子邮件”,并为您处理验证。

 <input type="email" name="email">

工作示例: https://www.w3schools.com/html/tryit.asp?filename=tryhtml_input_email