验证输入的电子邮件

发布于 2024-08-14 16:51:29 字数 133 浏览 8 评论 0 原文

在我的页面上,用户(内部员工)可以键入逗号分隔的电子邮件地址列表。我想做的是,每次输入逗号时,它都会检查新写入的电子邮件地址是否在地址簿中。

目前,我将地址簿存储为哈希表,搜索时间为 O(1),但如果建议,我可以轻松切换到其他数据结构。

On my page a users (internal staff members) can type a comma separated list of e-mail addresses. What I would like to do is every time a comma is typed it checks that the newly written e-mail address is in the address book.

Currently I have the address book stored as a Hashtable for O(1) search times but I can easily switch to another data structure if it is recommended.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

沉溺在你眼里的海 2024-08-21 16:51:29

您可以使用 JavaScript 和 AJAX 与服务器端进行通信。

您可以执行以下步骤:

  1. 创建一个 Web 服务,从客户端获取字符串(即用户输入的电子邮件),检查它并返回 true/false。
  2. 添加 [ScriptService] 属性
    网络服务类。
  3. 在页面上添加一个ASP.NET
    ScriptManager 控件
    Scripts/ScriptReference 指向步骤 1 中的 Web 服务。
  4. 在页面上,添加挂钩电子邮件文本框的 onkeydown 事件的 javascript 代码。
  5. 在此事件处理程序中,如果用户键入逗号,则向服务器执行 Web 服务请求与文本框值。当收到响应(真或假)时,用它做任何你需要的事情。

您可以在 MSDN 此处此处
您可能还会发现 AutoComplete AJAX 扩展程序很有帮助。

You can do that with JavaScript and AJAX to communicate with the server side.

You can do the next steps:

  1. Create a web service that gets the string from the client (which is the email the user has typed), checks it and returns true/false.
  2. Add [ScriptService] attribute to the
    web service class.
  3. On the page, add an ASP.NET
    ScriptManager control with
    Scripts/ScriptReference that points to the web service from step 1.
  4. On the page, add javascript code that hooks to the onkeydown event of the emails textbox
  5. In this event handler, if the user types a comma, execute a web service request to the server with the textbox value. When the respond (true or false) is received, do whatever you need with it.

You can read more on MSDN here and here.
You might also find helpful the AutoComplete AJAX extender.

旧街凉风 2024-08-21 16:51:29

为了在按键上完成它,将涉及 javascript(或客户端 vbscript,如果您使用 IE)。如果您希望基于按键输入来完成此操作,则没有它就无法完成此操作。

如果您要在用户离开该文本框时执行此操作,那么您可以使用 AutoPostback 并在服务器端用 C# 对其进行编码 - 但我不得不说,我认为这种方法很丑陋。在我看来,要求同步回发来验证数据对用户来说是一个巨大的负担,因此在您使用异步脚本来完成工作时应该只是最后的手段或权宜之计。您也可以在发布时(当他们尝试提交表单时)执行此操作并验证它们,并在任何地址失败时向用户返回验证消息,但再一次,这是同步的,用户不会获得早期反馈的好处。

将使用 Windows 服务(或使用 RESTful 服务)和 JavaScript 调用(使用 XmlHttpRequest 对象)来完成此操作。将文本框的 keydown 事件绑定到 JavaScript 方法。

<input type="text" onKeyDown="javascript:CheckInput(this)" />

然后设置您的 javascript 调用 - 这是正在发生的事情的核心:(

免责声明:此代码尚未生产就绪,它只是一个应该为您提供一些指导的示例)

var req;

function CheckInput()
{
    if (window.event) keycode = window.event.keyCode;
    if (keycode == 188) //KeyCode 188 = Comma;
    {

        //I haven't provided code for this, you will need to code
        //the extraction of the address you wish to test for yourself...
        var addressToCheck = ParseLastAddressFromTextBox(); 

        req = new XMLHttpRequest();
        //Replace <SERVICEADDRESS> with the URL of your web service
        //the 'true' parameter specifies that the call should be asynchronous
        req.open("POST", "<SERVICEADDRESS>", true);
        req.onreadystatechange = MatchSearchComplete; //This is the callback method
        //Set up the post headers
        req.setRequestHeader("Host", "localhost");
        req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        var params = addressToCheck;
        req.setRequestHeader("Content-Length", params.length);
        //Iitiate the call to the service
        req.send(params);        }
}

//The XMLHttpRequest object will fire this method when the
//onreadystatechange event fires...
function MatchSearchComplete()
{
    //Check that the response has the correct state and status
    //indicating that we've received a response from the server.
    if (req.readyState == 4 && req.status == 200)
    {
        //Our call to the service is complete
        var result = req.responseText;
        if (result == "false")
            alert('The address "'+ req.params +'" is not valid.);
    }
}

所以您在这里要做的是设置一个 XMLHttpRequest,将数据推送到其中并将其发送到 Web 服务。

您的 ASP.NET Web 应用程序需要内置一个 Web 服务来验证地址。

我还可能警告:如果只有一个地址并且用户没有点击逗号怎么办?您应该将 CheckInput() 方法的核心移到它自己的解析地址的方法中。 KeyDown 方法实际上应该只检查“,”是否被按下。这样您就可以调用 Web 服务来检查文本框何时失去焦点。我还会担心在用户不再次点击逗号的情况下修改现有地址。因此,我想知道这种方法。我认为,一旦地址经过验证,您应该有一个 javascript,它只允许删除该地址,它不应该是可编辑的...无论您采取什么方法,我只是警告您,仅检查会出现一些问题他们使用输入的“,”方法。

In order for it to be done on keypress there is going to be javascript (or client side vbscript if you're using IE) involved. This cannot be done without it if you're looking to do it based on keyed input.

If you were to do it when the user leaves that text box, then you could use AutoPostback and code it in C# on the server side - I have to say though, I think that approach is ugly. Requiring a synchronous postback to validate data is a huge imposition on the user in my opinion and therefore should only really be a last resort or a stopgap while you're getting asynchronous script to do the work. You could also do it at post time (when they're trying to submit the form) and validate them and give the user back a validation message if any of the addresses fail, but once again, this is synchronous and the user doesn't get the benefit of early feedback.

I would do this using either a Windows Service (or use RESTful service) using a javascript call (using the XmlHttpRequest object). Bind your textbox's keydown event to a JavaScript method.

<input type="text" onKeyDown="javascript:CheckInput(this)" />

Then set up your javascript call - this is the guts of what's going on:

(Disclaimer: This code is not production ready, it's merely an example that should give you some direction)

var req;

function CheckInput()
{
    if (window.event) keycode = window.event.keyCode;
    if (keycode == 188) //KeyCode 188 = Comma;
    {

        //I haven't provided code for this, you will need to code
        //the extraction of the address you wish to test for yourself...
        var addressToCheck = ParseLastAddressFromTextBox(); 

        req = new XMLHttpRequest();
        //Replace <SERVICEADDRESS> with the URL of your web service
        //the 'true' parameter specifies that the call should be asynchronous
        req.open("POST", "<SERVICEADDRESS>", true);
        req.onreadystatechange = MatchSearchComplete; //This is the callback method
        //Set up the post headers
        req.setRequestHeader("Host", "localhost");
        req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        var params = addressToCheck;
        req.setRequestHeader("Content-Length", params.length);
        //Iitiate the call to the service
        req.send(params);        }
}

//The XMLHttpRequest object will fire this method when the
//onreadystatechange event fires...
function MatchSearchComplete()
{
    //Check that the response has the correct state and status
    //indicating that we've received a response from the server.
    if (req.readyState == 4 && req.status == 200)
    {
        //Our call to the service is complete
        var result = req.responseText;
        if (result == "false")
            alert('The address "'+ req.params +'" is not valid.);
    }
}

So what you're doing here is setting up an XMLHttpRequest, pushing the data into it and firing it off to a web service.

Your ASP.NET web application will need a web service built in to it that will be doing the validation of the address.

I might also warn: What if there's only a single address and the user doesn't hit the comma? You should move the guts of the CheckInput() method out to it's own method which parses the addresses. The KeyDown method really should only check if the ',' was pushed. This way you can call the web service to check when the textbox loses focus also. I would also worry about modification of existing addresses without the user hitting the comma again. Therefore, I would wonder about this approach. I would consider that once an address is validated you should have a javascript that only allows that address to be deleted, it shouldn't be editable... whatever approach you take, I'm just warning you that there's some issue with just checking them using the entered ',' approach.

情释 2024-08-21 16:51:29

您必须使用 JavaScript 来完成此操作。输入逗号后,才可以将电子邮件传回 C# 后端进行验证。

You have to do this with Javascript. After the comma is typed, only then you can pass the email back to C# backend for verification.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文