电子邮件地址或域名的Jquery验证

时间:2014-02-19 08:35:31

标签: jquery jquery-validate

用户可以输入电子邮件地址或域名。我如何使用jQuery Validation Plugin验证这一点?

可接受的值:电子邮件地址/域名

例如:test@example.comsample.com

我需要在同一个文本框中验证域名。

$(document).ready(function(){
    jQuery.validator.addMethod("valDomain",function(nname)
    {
        name = nname.replace('http://','');
        nname = nname.replace('https://','');

        var arr = new Array(
                    '.com','.net','.org','.biz','.coop','.info','.museum','.name',
                    '.pro','.edu','.gov','.int','.mil','.ac','.ad','.ae','.af','.ag',
                    '.ai','.al','.am','.an','.ao','.aq','.ar','.as','.at','.au','.aw',
                    '.az','.ba','.bb','.bd','.be','.bf','.bg','.bh','.bi','.bj','.bm',
                    '.bn','.bo','.br','.bs','.bt','.bv','.bw','.by','.bz','.ca','.cc',
                    '.cd','.cf','.cg','.ch','.ci','.ck','.cl','.cm','.cn','.co','.cr',
                    '.cu','.cv','.cx','.cy','.cz','.de','.dj','.dk','.dm','.do','.dz',
                    '.ec','.ee','.eg','.eh','.er','.es','.et','.fi','.fj','.fk','.fm',
                    '.fo','.fr','.ga','.gd','.ge','.gf','.gg','.gh','.gi','.gl','.gm',
                    '.gn','.gp','.gq','.gr','.gs','.gt','.gu','.gv','.gy','.hk','.hm',
                    '.hn','.hr','.ht','.hu','.id','.ie','.il','.im','.in','.io','.iq',
                    '.ir','.is','.it','.je','.jm','.jo','.jp','.ke','.kg','.kh','.ki',
                    '.km','.kn','.kp','.kr','.kw','.ky','.kz','.la','.lb','.lc','.li',
                    '.lk','.lr','.ls','.lt','.lu','.lv','.ly','.ma','.mc','.md','.mg',
                    '.mh','.mk','.ml','.mm','.mn','.mo','.mp','.mq','.mr','.ms','.mt',
                    '.mu','.mv','.mw','.mx','.my','.mz','.na','.nc','.ne','.nf','.ng',
                    '.ni','.nl','.no','.np','.nr','.nu','.nz','.om','.pa','.pe','.pf',
                    '.pg','.ph','.pk','.pl','.pm','.pn','.pr','.ps','.pt','.pw','.py',
                    '.qa','.re','.ro','.rw','.ru','.sa','.sb','.sc','.sd','.se','.sg',
                    '.sh','.si','.sj','.sk','.sl','.sm','.sn','.so','.sr','.st','.sv',
                    '.sy','.sz','.tc','.td','.tf','.tg','.th','.tj','.tk','.tm','.tn',
                    '.to','.tp','.tr','.tt','.tv','.tw','.tz','.ua','.ug','.uk','.um',
                    '.us','.uy','.uz','.va','.vc','.ve','.vg','.vi','.vn','.vu','.ws',
                    '.wf','.ye','.yt','.yu','.za','.zm','.zw');

        var mai = nname;
        var val = true;

        var dot = mai.lastIndexOf(".");
        var dname = mai.substring(0,dot);
        var ext = mai.substring(dot,mai.length);
                    //alert(ext);

        if(dot>2 && dot<57)
        {
            for(var i=0; i<arr.length; i++)
            {
                if(ext == arr[i])
                {
                    val = true;
                    break;
                }
                else
                {
                    val = false;
                }
            }
            if(val == false)
            {
                return false;
            }
            else
            {
                for(var j=0; j<dname.length; j++)
                {
                    var dh = dname.charAt(j);
                    var hh = dh.charCodeAt(0);
                    if((hh > 47 && hh<59) || (hh > 64 && hh<91) || (hh > 96 && hh<123) || hh==45 || hh==46)
                    {
                        if((j==0 || j==dname.length-1) && hh == 45)
                        {
                            return false;
                        }
                    }
                    else    {
                        return false;
                    }
                }
            }
        }
        else
        {
            return false;
        }
        return true;
    }, 'Invalid domain name.');

    $("#form_search").validate({
        rules: {
            field: { required: true, valDomain:true }
        }
    });
});

<form id='form_search' method='POST'>
    <input type='email' id='search' name='search' class='required email valDomain' />
    <input type='submit' value='Search' />
</form>

2 个答案:

答案 0 :(得分:3)

添加验证方法,检查值是否为电子邮件/网址。

jQuery.validator.addMethod("emailordomain", function(value, element) {
  return this.optional(element) || /^\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,3}$/.test(value) || /[-a-zA-Z0-9@:%_\+.~#?&//=]{2,256}\.[a-z]{2,4}\b(\/[-a-zA-Z0-9@:%_\+.~#?&//=]*)?/.test(value);
}, "Please specify the correct url/email");


$( "#myform" ).validate({
  rules: {
    field: {
      required: true,
      emailordomain: true
    }
  }
});

您可以使用自己的正则表达式替换:)

<强> DEMO

答案 1 :(得分:-1)

你可以用简单的javascript +正则表达式

来做到这一点
function function validateMail(email) {
        var expr = /^([a-zA-Z0-9_.+-])+\@(([a-zA-Z0-9-])+\.)+([a-zA-Z0-9]{2,4})+$/;
        return expr.test(email); 
    }