简体   繁体   English

如何在 asp.net c# 中对电子邮件和联系人(相同的 TextBox)使用多客户端验证?

[英]How to use multiple client side validation for both email and contact (same TextBox ) in asp.net c#?

how to use one TextBox and multiple validations i tried this如何使用一个TextBox和多个验证我试过这个

private Boolean checkemail() // for checking email in database    
{
    SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);//sql connection string
    Boolean emailavailable = false;
    String myquery = "Select * from [test].[dbo].[MYFORM] where email='"+ TXTEmail.Text+"'";

    SqlCommand cmd = new SqlCommand();
    cmd.CommandText = myquery;
    cmd.Connection = conn;
    SqlDataAdapter da = new SqlDataAdapter();
    da.SelectCommand = cmd;
    DataSet ds = new DataSet(); //dataset
    da.Fill(ds);
    if (ds.Tables[0].Rows.Count > 0)
    {
        emailavailable = true;
    }
    conn.Close();
    return emailavailable;
}

c# code for the email check in database用于在数据库中检查电子邮件的 C# 代码

First point: let's get back to the basics: js validation第一点:让我们回到基础: js验证

Server side validation is performed by a web server, after input has been sent to the server.服务器端验证由 Web 服务器在输入发送到服务器后执行。

Client side validation is performed by a web browser, before input is sent to a web server.在将输入发送到 Web 服务器之前,客户端验证由 Web 浏览器执行。

For example: Client side validation would include email formating (is it a valid email?) and checks like empty fields that the server needs etc.例如:客户端验证将包括电子邮件格式(它是有效的电子邮件吗?)并检查服务器需要的空字段等。

Server side validation would check that the email is not yet used in another form by another user (like your case here) and it occurs in your backend system.服务器端验证将检查电子邮件是否尚未被其他用户以另一种形式使用(例如您在此处的情况),并且它发生在您的后端系统中。

Second point: SqlInjection.第二点: SqlInjection。 As mentioned in the comments, use parameters for sql sanitization.如评论中所述,使用参数进行 sql 清理。 It's a pretty basic exploit.这是一个非常基本的漏洞利用。

private Boolean checkemail() // for checking email in database    
{
    SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);//sql connection string
    Boolean emailavailable = false;
    String myquery = "Select * from [test].[dbo].[MYFORM] where email = @email";

    SqlCommand cmd = new SqlCommand();
    cmd.Parameters.Add("@email", SqlDbType.Text);
    cmd.Parameters["@email"].Value = TXTEmail.Text;
    cmd.CommandText = myquery;
    cmd.Connection = conn;
    SqlDataAdapter da = new SqlDataAdapter();
    da.SelectCommand = cmd;
    DataSet ds = new DataSet(); //dataset
    da.Fill(ds);
    if (ds.Tables[0].Rows.Count > 0)
    {
        emailavailable = true;
    }
    conn.Close();
    return emailavailable;
}

Third point: Multiple checks第三点:多重检查

If I understand what you are saying, you want to query with two parameters.如果我明白你在说什么,你想用两个参数进行查询。 Use the sql or operator like this:像这样使用 sql 或运算符:

String myquery = "Select * from [test].[dbo].[MYFORM] where email = @email or contact = @contact";
cmd.Parameters.Add("@email", SqlDbType.Text);
cmd.Parameters["@email"].Value = TXTEmail.Text;
cmd.Parameters.Add("@contact ", SqlDbType.Text);
cmd.Parameters["@contact "].Value = TXTEmail.Text;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用客户端验证来处理 c# 中的 asp.net mvc? - How to use client-side validation to work on your asp.net mvc in c#? 在电子邮件正文中发送图像,Android作为客户端,Asp.net c#作为服务器端 - Sending image in email body, Android as client side and Asp.net c# as server side 如何使用C#或ASP.net从客户端调用私有void按键事件处理程序,以仅允许数字进入文本框 - How to call a private void keypress event handler from client side using C# or ASP.net to allow only numbers into textbox 如何在 ASP.net C# 中使用两个不同的文本 colors - How to use two different colors of text in same TextBox TextMode = "MultiLine" in ASP.net C# 如何请求客户端确认在服务器端执行操作(如发送电子邮件)ASP.NET C# - How can I ask for client confirmation to perform an action (like sending an email) on server side ASP.NET C# 在C#ASP.NET Core 2.2的客户端上实现简单的验证 - Implementing simple validation on the client-side in C# ASP.NET Core 2.2 文本框的C#Asp.net验证方法? - c# Asp.net Validation Methods for textbox? 如何对控件使用JavaScript验证和asp.net验证 - How to use the both JavaScript validation and asp.net validation for a control ASP.NET Core MVC-包含多个步骤的表单-如何在每个步骤中触发客户端验证 - ASP.NET Core MVC - Form With Multiple Steps - How to Fire Client Side Validation At Each Step 客户端和服务器端的Asp.net C#加密/解密 - Asp.net C# Encryption/Decryption on Client and server Side
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM