码迷,mamicode.com
首页 > 其他好文 > 详细

表单操作集合类.

时间:2014-07-23 15:21:16      阅读:245      评论:0      收藏:0      [点我收藏+]

标签:blog   java   color   os   width   io   

/**
 * @class FormOperation 表单操作方法集合类
 * @constructor
 */
function FormOperation(){
    var TIP_CLASS_NAME = "tip-messages"; //消息显示框类
    var TIP_SHOW_TIME = 5000; //消息框显示时间
    /**
     * 检测必须字段
     * @param{String} className 类名
     * @param{Boolean} showTip 是否显示提示框,默认true
     * @returns {boolean} 检测通过返回true,未通过返回false
     */
    this.requiredField = function(className,showTip){
        var rf,rfCur;
        if(showTip===undefined)showTip = true;
        if($("."+className).length<1)return true;
        rf=$("."+className);
        for(var i=0;i<rf.length;i++){
            rfCur = $(rf[i]);
            if($.trim(rfCur.val())==""||$.trim(rfCur.val())==rfCur.attr("placeholder")){
                if(showTip){
                    this.showTip(rfCur,TIP_CLASS_NAME,false,0);
                }
                rfCur.focus();
                return false;
            }
        }
        return true;
    }
    /**
     * 比较字段值
     * @param{String} className 类名
     * @param{Boolean} showTip 是否显示提示框,默认true
     * @returns {boolean} 比较未通过返回false,比较通过返回true
     */
    this.compareField = function(className,showTip){
        var cf,cfCur,curVal;
        if(showTip===undefined)showTip = true;
        if($("."+className).length<1)return true;
        cf=$("."+className);
        for(var i= 0,len = cf.length;i<len;i++){
            cfCur = $(cf[i]);
            if((i===0)&&(curVal=cfCur.val()));
            if(curVal!==cfCur.val()){
                if(showTip){
                    this.showTip(cfCur,TIP_CLASS_NAME,false,1);
                }
                cfCur.focus();
                return false;
            }
        }
        return true;
    }
    /**
     * 特殊字段检测
     * @param{String} className 类名
     * @param{String} specialType 特殊字段类型,当前可选类型有:email/邮箱
     * @param{Boolean} showTip 是否显示提示框,默认true
     * @returns {boolean} 通过,返回true,不通过,返回false
     */
     this.specialField = function(className,specialType,showTip){
        var sf,sfVal,reg;
        if(showTip===undefined)showTip = true;
        if($("."+className).length<1)return true;
        sf=$("."+className);
        sfVal = $.trim(sf.val());
        if(specialType=="email"){
            reg=/^\w+((-\w+)|(\.\w+))*\@{1}\w+\.{1}\w{2,4}(\.{0,1}\w{2}){0,1}/ig;
            if(sfVal.search(reg)===-1) {
                if(showTip){
                    this.showTip(sf,TIP_CLASS_NAME,false,1);
                }
                sf.focus();
                return false;
            }
        }
        return true;
    }
    /**
     * 范围字段匹配
     * @param{String} className 类名
     * @param{String} specialType 特殊字段类型,当前可选类型有:email/邮箱
     * @param{Boolean} showTip 是否显示提示框,默认true
     * @returns {boolean} 通过,返回true,不通过,返回false
     */
    this.scopeField = function(className,showTip,minNumber,maxNumber){
        var sf,sfVal;
        if(showTip===undefined)showTip = true;
        if($("."+className).length<1)return true;
        sf=$("."+className);
        if(minNumber===undefined)minNumber=0;
        if(maxNumber===undefined)maxNumber=1000000000000;
        sfVal = $.trim(sf.val());
        if(minNumber<=sfVal.length&&sfVal.length<=maxNumber)
        {
            return true;
        }else{
            if(showTip){
                this.showTip(sf,TIP_CLASS_NAME,false,1);
            }
            sf.focus();
            return false;
        }
    }
    /**
     * 生成提示框
     * @param{Object} obj 待显示提示框元素对象
     * @param{String} tipClassName 提示框类名
     * @param{Boolean} autoHide 是否自动隐藏,默认true
     * @param{Number} msgType 消息类型,1/特殊消息|2/其它消息,默认1
     * @returns {*|jQuery|HTMLElement} 不自动显示时,返回提示框对象
     */
    this.showTip = function(obj,tipClassName,autoHide,msgType){
        var pY,pX,oW,msg,msgIndex;
        if((autoHide===undefined)&&(autoHide = true));
        if(obj){
            pY = obj.position().top;
            pX = obj.position().left;
            oW = obj.outerWidth();
//            alert(pY+" "+pX);
            $("."+tipClassName).text(this.getTipMsg(obj,"formData",msgType)).css({top:pY+4+"px",left:pX+oW+"px"}).show();
            if(autoHide){
                setTimeout(function(){
                    $("."+tipClassName).hide();
                },TIP_SHOW_TIME);
            }else{
                return $("."+tipClassName);
            }
        }
    }
    /**
     * 提取消息
     * @param{Object} obj 将要提取消息元素
     * @param{String} objAttr 消息来源属性名,默认formData
     * @param{Number} msgType 消息类别 0/常规消息|1/特殊消息 默认0
     * @returns {string|*} 目标消息
     */
    this.getTipMsg = function(obj,objAttr,msgType){
        var msg,msgLength,msgIndex;
        if(obj===undefined)return;
        if(objAttr===undefined)objAttr = "formData";
        if(msgType===undefined)msgType = 0;
        msg = $.trim($(obj).attr(objAttr));
        msgIndex = msgLength = msg.length;
        if((msg.indexOf("&")!==-1)&&(msgIndex=msg.indexOf("&")));
        switch (msgType){
            case 0: msg = msg.substr(0,msgIndex);break;
            case 1: msg = msg.substr(msgIndex+1,msgLength);break;
        }
        return msg;
    }
    /**
     * 文本框文字回显(仅作用于不支持“placeholder”属性浏览器)
     * @param{String} className 文本框类名
     * @param{String} replaceColorClassName 文字颜色替换类名
     */
    this.echo = function(className,replaceColorClassName){
        var echoObj,echoText,echoCur;
        if (!(‘placeholder‘ in document.createElement(‘input‘))){
            if(!(echoObj = $("."+className))) return;
            $.each(echoObj,function(i,obj){
                $(obj).val($(obj).attr("placeholder")).addClass(replaceColorClassName);
            });
            echoObj.focus(function(){
                echoCur = $(this);
                echoText = echoCur.removeClass(replaceColorClassName).attr("placeholder");
                if($.trim(echoCur.val())==echoText&&echoCur.val(""));
            }).focusout(function(){
                if(($.trim(echoCur.val())==""||$.trim(echoCur.val())==echoText)&&echoCur.val(echoText).addClass(replaceColorClassName));
            });
        }
    }

}

  

表单操作集合类.,布布扣,bubuko.com

表单操作集合类.

标签:blog   java   color   os   width   io   

原文地址:http://www.cnblogs.com/xiaozweb/p/3862960.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!