搜索嵌入的电子邮件和电话号码
我需要使用 JavaScript 表单验证例程来扫描各种输入文本字段以查找嵌入的电话号码和电子邮件地址。这是针对免费发布但与买家“付费联系”的分类系统,因此目的是(尽可能)防止用户(发布广告的人)简单地嵌入他们的手机和/或通过电子邮件发送联系信息以绕过系统。
我已经在谷歌上搜索了一段时间,正则表达式不是我的强项,所以我很难找到一段好的代码来提供帮助。我想做的就是获得一个文本字段的通过/失败(如果它似乎没有嵌入电子邮件和/或电话号码,则通过,如果有,则失败)
是否有人已经为此提供了良好的 javascript 解决方案?
I need to use a javascript form validation routine to scan various input text fields for embedded phone numbers and email addresses. This is for a classifieds system that is free to post but 'pay to connect' with buyers, so the intent is to prevent (as much as possible) the ability for users (those posting the ad) from simply embedding their phone and/or email contact information to bypass the system.
I've been googling for awhile now, and RegEx is not my strong suit, so I'm having a bit of a hard time finding a good snippet of code to help. All I want to do is get a pass/fail for a text field (pass if it does not appear to have embedded email and/or phone numbers, and fail if it does)
Does anyone already have a good javascript solution for this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
试试这个:
Try this:
感谢大家的意见。这是我最终得到的版本,希望对其他人有帮助。注意:我删除了此帖子中实际的“坏”字样,以便它能够通过该网站的过滤器。您可以将“badword1”、“badword2”等替换为实际的“坏”单词(您知道,例如 nukular、calender、ekcetera):
Thanks to all for the input. Here is the version I ended up with, hope it helps someone else. Note: I removed the actual 'bad' words for this posting so that it would pass this site's filters. You can replace 'badword1', 'badword2', etc. with actual 'bad' words (you know, like nukular, calender, ekcetera):
这将找到电子邮件地址:\b[A-Z0-9._%+-]+@[A-Z0-9.-]+.[AZ]{2,4}\b
这将找到电话号码: \b(()?\d{2,3}(?(1)))(?:-?\d{3}-?\d{4}|\d{11} )\b
This will find email addresses: \b[A-Z0-9._%+-]+@[A-Z0-9.-]+.[A-Z]{2,4}\b
and this will find phone numbers: \b(()?\d{2,3}(?(1)))(?:-?\d{3}-?\d{4}|\d{11})\b
您将能够得到一些,但不要期望得到大部分(特别是当人们意识到这一要求,或者有不止一次机会填写表格时)。
人们已经非常擅长通过执行诸如“myaddresses at hotmail dot com”之类的操作来规避电子邮件地址的机器人检测,并且这种方式有上百万种变体。此外,电话号码因地区而异。
You'll be able to get some, but don't expect to get most (especially if people are aware of the requirement, or get more than one chance to fill the form).
People are already really good at circumventing bot detection of email addresses by doing things like "myaddresses at hotmail dot com", and there are a million variations of this. Also, Phone numbers vary by region.
您没有说明您正在使用什么服务器端技术,但最好在服务器上进行此类处理。在我自己的工作中(ASP.NET),我总是偏爱服务器端,因为面向对象的服务器端框架的灵活性和功能几乎每次都会胜过 JavaScript。这种情况也不例外,因为 JavaScript 正则表达式支持 似乎缺少几个关键特征。
无论您选择服务器端还是客户端,我发现使用 浓缩咖啡。如果您在 Mac 上运行,请考虑 Reggy。这些工具通常附带几个“常用”正则表达式,用于各种常见查询(即电话号码、电子邮件等),通常只需进行最少的修改即可工作。
You don't say what server side technology you're using, but it might be preferable to do this type of processing on the server. I always favor server side in my own work (ASP.NET), because the flexibility and power of an object oriented server side framework will trump that of JavaScript just about every time. This case is no exception, as it appears that JavaScript regular expression support is lacking several key features.
Regardless of whether you choose to go server side or client side, I've found that writing RegEx code is much simplified when using a tool such as Espresso. If you're running on a Mac, consider Reggy. These tools usually come with several "stock" RegEx expressions for various common queries (i.e. phone numbers, email etc) that usually work with minimal modification.