javascript和正则表达式之间的区别以及用于电子邮件验证的filter

系统教程 行业动态 更新时间:2024-06-14 17:00:14
javascript和正则表达式之间的区别以及用于电子邮件验证的filter_var(difference between a javascript and regular expression and filter_var for email validation)

我一直在使用javascript和filter_var进行一些电子邮件验证 使用preg_match。使用必要的参数和正则表达式。因为输入sanitaisation已经长期存在,需要记住很多事情,在验证电子邮件时要使用。

for preg_match if(!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email))

通过javascript

function validateForm() { var x=document.forms["myForm"]["email"].value; var atpos=x.indexOf("@"); var dotpos=x.lastIndexOf("."); if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length) { alert("Not a valid e-mail address"); return false; } }

通过filter_var

<?php if(!filter_var("someone@example....com", FILTER_VALIDATE_EMAIL)) { echo("E-mail is not valid"); } else { echo("E-mail is valid"); } ?>

i have been thnking about some validation of email using javascript in one hand and filter_var with neccessary parameters and regular expression using preg_match .now as input sanitaisation has gone in a long run with so many things to keep in mind, wht to use when validating email.

for preg_match if(!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email))

by javascript

function validateForm() { var x=document.forms["myForm"]["email"].value; var atpos=x.indexOf("@"); var dotpos=x.lastIndexOf("."); if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length) { alert("Not a valid e-mail address"); return false; } }

by filter_var

<?php if(!filter_var("someone@example....com", FILTER_VALIDATE_EMAIL)) { echo("E-mail is not valid"); } else { echo("E-mail is valid"); } ?>

最满意答案

是的,这三种变体是非常不同的。

preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email)

这个正则表达式非常严格 - 实际上,它的限制性太强了。 它只允许@ -sign和dot周围的单词字符和缩写。 例如,它甚至不允许子域(这是很常见的)。

var atpos = email.indexOf("@"); var dotpos = email.lastIndexOf("."); return !(atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length)

这看起来很不错。 它检查是否存在@和a . 以正确的顺序,这足以识别类似电子邮件的字符串。 它可以用正则表达式/.+@.+\..+/.test(email)代替(这不允许/.+@.+\..+/.test(email) ,但没关系)。

filter_var($email, FILTER_VALIDATE_EMAIL)

这可能是在PHP中实现它的最佳方式,但请注意,它也存在一些 缺陷 。

我还建议使用复杂的正则表达式停止验证电子邮件地址 :-)

Yes, those three variants are very different.

preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email)

This regex is very restrictive - actually, it's too restrictive. It does only allow word characters and minuses around the @-sign and dot. For example, it does not even allow subdomains (which are quite common).

var atpos = email.indexOf("@"); var dotpos = email.lastIndexOf("."); return !(atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length)

This looks quite well. It checks for the existence of an @ and a . in the correct order, which is enough to identify email-like looking strings. It could be replaced by the regular expression /.+@.+\..+/.test(email) (which would not allow linebreaks, but that's fine).

filter_var($email, FILTER_VALIDATE_EMAIL)

This is probably the best way to do it in PHP, but notice that is has some flaws as well.

I would also recommend the article Stop Validating Email Addresses With Complicated Regular Expressions :-)

更多推荐

本文发布于:2023-04-18 00:52:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/f7329abdf52b84ded98ed524c2cb4efd.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:电子邮件   区别   正则表达式   javascript   filter

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!