欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JQuery 簡(jiǎn)便實(shí)現(xiàn)頁(yè)面元素?cái)?shù)據(jù)驗(yàn)證功能

 更新時(shí)間:2007年03月24日 00:00:00   作者:  
ASP.NET提供了豐富的數(shù)據(jù)驗(yàn)證控件,但這個(gè)驗(yàn)證功能都必須運(yùn)用在服務(wù)器控件中;在復(fù)合驗(yàn)證數(shù)據(jù)時(shí)使用也不是很方便(雖然拖放控件很方便,但拖放數(shù)增多和設(shè)置相關(guān)屬性就變得很麻煩的事情)。為了更方便靈活地實(shí)現(xiàn)驗(yàn)證功能,于是采用了JQuery編寫(xiě)了一個(gè)簡(jiǎn)單驗(yàn)證組件,在使用時(shí)只需要簡(jiǎn)單的描述一下驗(yàn)證規(guī)則,并不需要寫(xiě)一句JavaScript就可以實(shí)現(xiàn)具備ASP.NET驗(yàn)證控件的功能(當(dāng)然頁(yè)面要引入相關(guān)JavaScript文件)。
功能目標(biāo)
       通過(guò)簡(jiǎn)單的自定義屬性值描述具體的驗(yàn)證功能,當(dāng)Form提交自動(dòng)攔載執(zhí)行驗(yàn)證功能。如果所有成員驗(yàn)證成功就提交,否則取消提交。
       簡(jiǎn)單使用描述: 
       
復(fù)制代碼 代碼如下:

<input id="Text1" type="text" validator="type:string;nonnull:true;tip:請(qǐng)輸入用戶名!;tipcontrol:nametip" /> 
       <input id="Text2" type="text" validator="type:date;nonnull:true;tip:請(qǐng)輸入正確的出生日期!;tipcontrol:birthtip;min:1950-1-1;max:2000-1-1" /> 

實(shí)現(xiàn)要點(diǎn)
       利用JQuery提供的素元查找功能,方便查找出需要驗(yàn)證的元素對(duì)象;通過(guò)$(document).ready事件進(jìn)很驗(yàn)證代碼的初始化和執(zhí)行的切入工作。
       定義驗(yàn)證規(guī)則描述屬性,在初始化時(shí)對(duì)相關(guān)驗(yàn)證描述進(jìn)行分解和集成處理。
       攔載Form提交過(guò)程進(jìn)行驗(yàn)證處理,根據(jù)情況確定是否提交數(shù)據(jù)。
       元素對(duì)象在發(fā)生onchange事件時(shí)自動(dòng)執(zhí)行驗(yàn)證處理功能。
       通過(guò)alert和自定義區(qū)域顯示錯(cuò)誤信息。
具體JavaScript代碼
       實(shí)現(xiàn)代碼并不復(fù)雜只有幾百行,由于相關(guān)代碼比較簡(jiǎn)單所以沒(méi)有編寫(xiě)注解.
具體代碼:
復(fù)制代碼 代碼如下:

// JScript 文件 
//<validator> 
//type:int|number|date|string 
//nonnull:true|false 
//regex:^[-\+]?\d+$ 
//min:0 
//max:999999999 
//campare:id 
//comparetype:eq,neq,leq,req,le,ri 
//tipcontrol: 
//tip: 
//</validator> 
var ErrorMessage; 
function FormValidate(form) 

    ErrorMessage=''; 
    var legality,items; 
    legality = true; 
    items = $(form).find("input[@validator]"); 
    for(var i =0;i< items.length;i++) 
    { 
       if(legality) 
       { 
            legality = OnItemValidator($(items[i])); 
       } 
       else 
       { 
            OnItemValidator($(items[i]));     
       } 
    } 
    items = $(form).find("textarea[@validator]"); 
    for(var i =0;i< items.length;i++) 
    { 
       if(legality) 
       { 
            legality = OnItemValidator($(items[i])); 
       } 
       else 
       { 
            OnItemValidator($(items[i]));     
       } 
    } 
    items = $(form).find("select[@validator]"); 
    for(var i =0;i< items.length;i++) 
    { 
       if(legality) 
       { 
            legality = OnItemValidator($(items[i])); 
       } 
       else 
       { 
            OnItemValidator($(items[i]));     
       } 
    } 
    if(!legality) 
    { 
        if(ErrorMessage !='') 
            alert(ErrorMessage); 
    } 
    return legality; 

function CreateValObject(validator) 

    var valobj = {  type:'string', 
                    nonnull:false, 
                    regex:null, 
                    min:null, 
                    max:null, 
                    campare:null, 
                    comparetype:null, 
                    tipcontrol:null, 
                    tip:null}; 
    var properties; 
    var execute; 
    var namevalue; 
    properties = validator.split(';'); 
    for(i=0;i<properties.length;i++) 
    { 
        namevalue = properties[i].split(':'); 
        execute ="valobj." + namevalue[0] +'=\''+ namevalue[1]+'\';'; 
        eval(execute); 
    } 
    return valobj; 

function OnItemValidator(control) 

    var regex,maxvalue,minvalue,cvalue; 
    var valobj = CreateValObject(control.attr('validator')); 
    var value = control.val(); 
    value = ValidatorConvert(value,valobj.type); 
    if(valobj.nonnull=="true") 
    { 
        if(value == null || value=="") 
        { 
            ValidatorError(valobj); 
            return false; 
        } 
    } 
    else 
    { 
        if(value == null || value=="") 
            return true; 
    } 
    if(valobj.regex != null) 
    { 
        regex =new RegExp(valobj.regex); 
        if(value.match(regex) == null) 
        { 
            ValidatorError(valobj); 
            return false; 
        } 
    } 
    if(valobj.min != null) 
    { 
        minvalue = ValidatorConvert(valobj.min,valobj.type); 
        if(!CompareValue(value,minvalue,"req")) 
        { 
            ValidatorError(valobj); 
            return false; 
        } 
    } 
    if(valobj.max != null) 
    { 
        maxvalue = ValidatorConvert(valobj.max,valobj.type); 
        if(!CompareValue(value,maxvalue,"leq")) 
        { 
            ValidatorError(valobj); 
            return false; 
        } 
    } 
    if(valobj.campare != null) 
    { 
        cvalue = $('#' + valobj.campare).val(); 
        cvalue = ValidatorConvert(cvalue,valobj.type); 
        if(!CompareValue(value,cvalue,valobj.comparetype)) 
        { 
            ValidatorError(valobj); 
            return false; 
        } 
    } 
    return true; 

function ValidatorError(valobj) 

    if(valobj.tipcontrol != null) 
        showTip($("#"+ valobj.tipcontrol)); 
    else 
    { 
        if(ErrorMessage !='') 
            ErrorMessage += '\n'; 
        ErrorMessage += valobj.tip; 
    } 

function CompareValue(leftvalue,rightvalue,compareType) 

    if(leftvalue == null || rightvalue == null) 
        return false; 
    if(compareType=="eq") 
    { 
        return leftvalue == rightvalue; 
    } 
    else if(compareType =="neq") 
    { 
        return leftvalue != rightvalue; 
    } 
    else if(compareType =="le") 
    { 
        return leftvalue < rightvalue; 
    } 
    else if(compareType =="leq") 
    { 
        return leftvalue <= rightvalue; 
    } 
    else if(compareType =="ri") 
    { 
        return leftvalue > rightvalue; 
    } 
    else if(compareType =="req") 
    { 
        return leftvalue >= rightvalue; 
    } 
    else 
    { 
        return false; 
    } 

function showTip(control) 

    if(control.attr('show') != 'on') 
    { 
        control.fadeIn("slow"); 
        control.attr('show','on'); 
    } 

function hideTip(control) 

    control.hide(); 
    control.attr('show',''); 

function ValidatorConvert(value, dataType) { 
    var num,exp,m; 
    var year,month,day 
    if(value == null || value =="") 
        return null; 
    if(dataType=="int") 
    { 
        exp=/^[-\+]?\d+$/; 
        if (value.match(exp) == null) 
            return null; 
        num = parseInt(value, 10); 
        return (isNaN(num) ? null : num); 
    } 
    else if(dataType =="number") 
    { 
        exp=/^[-\+]?((\d+)|(\d+\.\d+))$/; 
        if (value.match(exp) == null) 
            return null; 
        num = parseFloat(value); 
        return (isNaN(num) ? null : num); 
    } 
    else if(dataType =="date") 
    { 
        exp=/^(\d{4})([-/]?)(\d{1,2})([-/]?)(\d{1,2})$/ 
        m = value.match(exp); 
        if (m == null) 
        { 
            exp=/^(\d{1,2})([-/]?)(\d{1,2})([-/]?)(\d{4})$/ 
            m = value.match(exp); 
            if(m== null) 
                return null; 
            year = m[5]; 
            month = m[1]; 
            day =m[3]; 
        } 
        else 
        { 
            year = m[1]; 
            month =m[3]; 
            day = m[5]; 
        } 
        try 
        { 
            num = new Date(year,month,day); 
        } 
        catch(e) 
        { 
            return null; 
        } 
        return num; 
    } 
    else 
    { 
        return value.toString(); 
    } 

$(document).ready( 
    function(){ 
        $("[@validator]").each(function(i) 
                    { 
                        var valobj = CreateValObject($(this).attr('validator')); 
                        if(valobj.tipcontrol !=null) 
                        { 
                            $('#' + valobj.tipcontrol).addClass('ErrorTip'); 
                            hideTip($('#' + valobj.tipcontrol)); 
                            $("#"+ valobj.tipcontrol).html("<NOBR>"+valobj.tip+"</NOBR>"); 
                        } 
                        $(this).change(function(){ 
                           if(OnItemValidator($(this))) 
                           { 
                                if(valobj.tipcontrol !=null) 
                                { 
                                   hideTip($('#' + valobj.tipcontrol));   
                                } 
                           } 
                           else 
                           { 
                                if(valobj.tipcontrol !=null) 
                                { 
                                   showTip($('#' + valobj.tipcontrol));   
                                } 
                           } 
                        }); 
                     } 
                ); 
          $("form").each(function(id) 
            { 
                $(this).submit(function(){return FormValidate(this)}); 
            } 
            ); 
    } 
); 

下載相關(guān)例程代碼

相關(guān)文章

最新評(píng)論