在 Javascript 中检查电子邮件地址
我想很多人以前都做过一些类似的开发任务:
我想检查人们的电子邮件地址是否只匹配@tomtom.com或@stream.com。
目前,我想到了两个解决方案:
使用
indexof()
函数var checkTomTomEmail=eo.data.username.indexOf("@tomtom.com"); var checkStreamEmail=eo.data.username.indexOf("@stream.com"); if (checkTomTomEmail > 0 || checkStreamEmail > 0 ) { //运行登录代码 } 别的 { //请使用您的tomtom或stream电子邮件登录 }
使用match
var patt1=/@tomtom.com/gi; var patt2=/@stream.com/gi; var checkTomTomEmail=eo.data.username.match(patt1); var checkStreamEmail=eo.data.username.match(patt2); if(indexOf(checkTomTomEmail)> 1 ||indexOf (checkStreamEmail)>1) { //登录 }
我仍然认为我还没有考虑所有细节。有什么建议吗?
谢谢
I think many people have done some similar development tasks before:
I would like to check the people's email address whether only match @tomtom.com or @stream.com.
Currently, I have two solutions in my mind:
Using
indexof()
functionvar checkTomTomEmail=eo.data.username.indexOf("@tomtom.com"); var checkStreamEmail=eo.data.username.indexOf("@stream.com"); if (checkTomTomEmail >0 || checkStreamEmail >0 ) { //Run the login code } Else { //Please login with your tomtom or stream email }
Using match
var patt1=/@tomtom.com/gi; var patt2=/@stream.com/gi; var checkTomTomEmail=eo.data.username.match(patt1); var checkStreamEmail=eo.data.username.match(patt2); if(indexOf(checkTomTomEmail)> 1 ||indexOf (checkStreamEmail)>1) { //Login }
I still think I do not consider all the detail yet. Any suggestions?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
也许,如果人们只允许输入这两个地址的电子邮件,您应该只收集用户名,然后允许他们使用单选按钮选择@tomtom.com 或@stream.com。
如果您仍然想走 javascript 路线,那么您的正则表达式可以合并到单个语句中
Perhaps if people are only allowed to enter emails for those two addresses you should only collect the username and then allow them to choose @tomtom.com or @stream.com using radiobuttons.
If you still want to go the javascript route then your regex can be combined into a single statement
这样怎么样...
我们可以执行 .test(...) 来查看是否有匹配项,而不是执行 .match(...) - 您将得到一个字符串。
此模式保证以下几点:
您可以通过以下方式进一步自定义:也就是说,确保用户名必须至少有 3 个字符,您可以在电子邮件地址中使用下划线或破折号等。
要回答您的问题,这两种解决方案都不起作用。原因:
有关正则表达式的更多信息:http://www.regular-expressions.info/reference.html
How about this...
Instead of performing a .match(...) - Which you'll get a string back, we can perform a .test(...) to see if anything matches.
This pattern guarantees the following:
You can customize this further by, saying, making sure username must have at least 3 characters, you can use underscore or dashes in the email address, etc.
To answer your question, both solutions won't work. Reasons:
More on Regex: http://www.regular-expressions.info/reference.html