js 常用校验

//检查必填字段  
//(校验必填项不能为空(包括input框,单选框,复选框,文本域))  
var elem = document.getElementById("age");  
function checkRequired(elem){  
        var name = elem.name;  
        if(elem.type == "checkbox" || elem.type == "radio"){  
            var results=[];  
            results.numChecked = 0;  
            var input = document.getElementsByTagName("input");  
            for(var i=0;i 0 && elem.value != elem.defaultValue;  
        }  
    }
//检查输入元素是否键入了信息的通用函数  
function checkRequired(elem){  
    if(elem.type == "checkbox" || elem.type == "radio")  
        return getInputsByName(elem.name).numChecked;  
    else   
        return elem.value.length > 0 && elem.value != elem.defaultValue;  
}  
//找出指定name的所有input元素(对查找 以及处理复选框或单选框十分有用)  
function getInputsByName(name){  
    var results=[];  
    results.numChecked = 0;  
    var input = document.getElementsByTagName("input");  
    for(var i=0;i 
  

 

你可能感兴趣的:(js 常用校验)