jQuery验证电子邮件地址或域名 [英] Jquery validation for email address or domain name

查看:443
本文介绍了jQuery验证电子邮件地址或域名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

用户可以输入电子邮件地址或域名.如何使用jQuery验证插件对此进行验证?

User can either enter a email address or a domain name. How do i validate this using jQuery Validation Plugin ?

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

Acceptable Value: Email Address/domain name

例如:test@example.comsample.com

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

I need to validate domain name in the same textbox.

$(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>

推荐答案

添加一种验证方法,该方法检查值是否为电子邮件/URL.

Add a validation method that checks if a value is an email/url.

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
    }
  }
});

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

You can substitute with your own regex :)

演示

DEMO

这篇关于jQuery验证电子邮件地址或域名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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