欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

jQuery封装(一):手机号码的的封装

程序员文章站 2022-03-27 08:57:22
...
(function($){
    //验证手机号码
    jQuery.validMobile = function(options) 
    {
        var defaluts = {id:''};
        var opts = $.extend(defaluts, options);
        var mobile = $("#"+opts.id).val();
        if(!$.isEmpty(mobile))
        {
            $.box({text:'手机号码不能为空'});
            return false;
        }

        if(mobile.length < 11)
        {
            $.box({text:'手机号码不能少于11位'});
            return false;
        }

        var myreg = 11&& /^((13|14|15|17|18)[0-9]{1}\d{8})$/;
        if (!myreg.test(mobile)) 
        {
            $.box({text:'手机号码格式不正确'});
            $("#"+opts.id).focus();
            return false;
        } 
        return true;
    };
})(jQuery);

辅助的js

(function($){

    //判断是否为空
    jQuery.isEmpty = function(options)
    {
        if(null == options || options == "" || options == undefined || options.length < 1 || options == false || options == "null")
        {
            return false
        }
        return true;
    }

        //页面提示窗
    jQuery.box = function(options)
    {
        var defaluts = {text:text,icon:type};
        var opts = $.extend(defaluts, options);
        url = "https://cdn.laicunba.com/yunact/20180426earlysummer/error.png";
        if(opts.icon == 1)
        {
            url = "https://cdn.laicunba.com/yunact/20180426earlysummer/success.png";
        }
       $("body").append("<div id='box' style='width:90%;height: 1.5rem;background:#FFF0EE;border-radius: 0.3rem ; position: fixed;top: 50%; left: 50%;-moz-transform: translate(-50%, -50%);-ms-transform: translate(-50%, -50%); -webkit-transform: translate(-50%, -50%); transform: translate(-50%, -50%);'>"+
           "<div style='position:relative'>"+
              "<img src='"+url+"' style='width:0.6rem;height:0.6rem;left:1rem;position:absolute;top:0.4rem'>"+
              "<span style='color:#C44949;font-size:0.39rem;margin-left:0.3rem;position:absolute;top:0.4rem;left:1.6rem'>"+opts.text+
           "</span>"+
           "</div>"+
        "</div>");
        setTimeout(function(){$("#box").remove();},3000)
    };
})(jQuery)

引用方法:

$.validMobile({id:'mobile'})