验证输入的电子邮件
在我的页面上,用户(内部员工)可以键入逗号分隔的电子邮件地址列表。我想做的是,每次输入逗号时,它都会检查新写入的电子邮件地址是否在地址簿中。
目前,我将地址簿存储为哈希表,搜索时间为 O(1),但如果建议,我可以轻松切换到其他数据结构。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
在我的页面上,用户(内部员工)可以键入逗号分隔的电子邮件地址列表。我想做的是,每次输入逗号时,它都会检查新写入的电子邮件地址是否在地址簿中。
目前,我将地址簿存储为哈希表,搜索时间为 O(1),但如果建议,我可以轻松切换到其他数据结构。
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(3)
您可以使用 JavaScript 和 AJAX 与服务器端进行通信。
您可以执行以下步骤:
网络服务类。
ScriptManager 控件
Scripts/ScriptReference 指向步骤 1 中的 Web 服务。
您可以在 MSDN 此处和此处。
您可能还会发现 AutoComplete AJAX 扩展程序很有帮助。
You can do that with JavaScript and AJAX to communicate with the server side.
You can do the next steps:
web service class.
ScriptManager control with
Scripts/ScriptReference that points to the web service from step 1.
You can read more on MSDN here and here.
You might also find helpful the AutoComplete AJAX extender.
为了在按键上完成它,将涉及 javascript(或客户端 vbscript,如果您使用 IE)。如果您希望基于按键输入来完成此操作,则没有它就无法完成此操作。
如果您要在用户离开该文本框时执行此操作,那么您可以使用 AutoPostback 并在服务器端用 C# 对其进行编码 - 但我不得不说,我认为这种方法很丑陋。在我看来,要求同步回发来验证数据对用户来说是一个巨大的负担,因此在您使用异步脚本来完成工作时应该只是最后的手段或权宜之计。您也可以在发布时(当他们尝试提交表单时)执行此操作并验证它们,并在任何地址失败时向用户返回验证消息,但再一次,这是同步的,用户不会获得早期反馈的好处。
我将使用 Windows 服务(或使用 RESTful 服务)和 JavaScript 调用(使用 XmlHttpRequest 对象)来完成此操作。将文本框的 keydown 事件绑定到 JavaScript 方法。
然后设置您的 javascript 调用 - 这是正在发生的事情的核心:(
免责声明:此代码尚未生产就绪,它只是一个应该为您提供一些指导的示例)
所以您在这里要做的是设置一个 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.
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)
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.
您必须使用 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.