在javascript中检测输入错误的电子邮件地址 [英] detecting mistyped email addresses in javascript

查看:75
本文介绍了在javascript中检测输入错误的电子邮件地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到有时用户会错误输入电子邮件地址(以联系方式),例如,键入@ yahho.com,@ yhoo.com或@ yahoo.co而不是@ yahoo.com

I notice sometimes users mistype their email address (in a contact-us form), for example, typing @yahho.com, @yhoo.com, or @yahoo.co instead of @yahoo.com

我认为可以使用一些JavaScript在现场纠正。只需检查电子邮件地址中是否存在可能的错误(例如上面列出的错误),以便用户输入his_email@yhoo.com时,可以显示一条不引人注意的消息,或类似的内容,表明他可能是@yahoo。 com,然后要求仔细检查他是否正确键入了电子邮件。

I feel that this can be corrected on-the-spot with some javascript. Simply check the email address for possible mistakes, such as the ones listed above, so that if the user types his_email@yhoo.com, a non-obtrusive message can be displayed, or something like that, suggesting that he probably means @yahoo.com, and asking to double check he typed his email correctly.

问题是:

我如何检测-在Java脚本中-字符串与 yahoo或 yahoo.com非常相似吗?还是一般来说,我该如何检测两个字符串之间的相似度?

The Question is:
How can I detect -in java script- that a string is very similar to "yahoo" or "yahoo.com"? or in general, how can I detect the level of similarity between two strings?

P.S。 (这是一个旁注)在我的特定情况下,用户不是讲英语的人,而且大多数人都不熟练,网站本身也不是英语。

P.S. (this is a side note) In my specific case, the users are not native English speakers, and most of them are no where near fluent, the site itself is not in English.

推荐答案

这是一个肮脏的实现,可以通过 Levenshtein距离 。 levenshteinenator的名称为 此链接 。您将想要的任何受欢迎的域添加到domains数组中,并检查输入的电子邮件的主机部分的距离是1还是2,这可以合理地假设某个地方有错字。

Here's a dirty implementation that could kind of get you some simple checks using the Levenshtein distance. Credit for the "levenshteinenator" goes to this link. You would add whatever popular domains you want to the domains array and it would check to see if the distance of the host part of the email entered is 1 or 2 which would be reasonably close to assume there's a typo somewhere.

levenshteinenator = function(a, b) {
    var cost;

    // get values
    var m = a.length;
    var n = b.length;

    // make sure a.length >= b.length to use O(min(n,m)) space, whatever that is
    if (m < n) {
        var c=a;a=b;b=c;
        var o=m;m=n;n=o;
    }

    var r = new Array();
    r[0] = new Array();
    for (var c = 0; c < n+1; c++) {
        r[0][c] = c;
    }

    for (var i = 1; i < m+1; i++) {
        r[i] = new Array();
        r[i][0] = i;
        for (var j = 1; j < n+1; j++) {
            cost = (a.charAt(i-1) == b.charAt(j-1))? 0: 1;
            r[i][j] = minimator(r[i-1][j]+1,r[i][j-1]+1,r[i-1][j-1]+cost);
        }
    }

    return r[m][n];
}

// return the smallest of the three values passed in
minimator = function(x,y,z) {
    if (x < y && x < z) return x;
    if (y < x && y < z) return y;
    return z;
}

var domains = new Array('yahoo.com','google.com','hotmail.com');
var email = 'whatever@yahoo.om';
var parts = email.split('@');
var dist;
for(var x=0; x < domains.length; x++) {
    dist = levenshteinenator(domains[x], parts[1]);
    if(dist == 1 || dist == 2) {
        alert('did you mean ' + domains[x] + '?');
    }
}

这篇关于在javascript中检测输入错误的电子邮件地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆