一个正则批量判断数据正确性的函数

//判断数据的正确性
 var idSpans= new Array();
 idSpans[0] = new Array("item_2","^[\\s\\S]{1,16}$","productName","产品名称应在1-16字以内","<img src='http://www.www.com/office/style/1/img/right.gif' border='0'>",true);
 idSpans[1] = new Array("item_3_2","^[\\s\\S]+$","iditem_3_2Spans","商机分类不能为空","<img src='http://www.www.com/office/style/1/img/right.gif' border='0'>",true);
 idSpans[2] = new Array("item_4","^[\\s\\S]+$","iditem_4Spans","产品系列不能为空","<img src='http://www.www.com/office/style/1/img/right.gif' border='0'>",true);
 idSpans[3] = new Array("item_5_1","^[\\s\\S]{2,30}$","iditem_5_1Spans","商机产地长度应在2-30字以内","<img src='http://www.www.com/office/style/1/img/right.gif' border='0'>",false);
 idSpans[4] = new Array("item_5_2","^[\\s\\S]{2,30}$","idItem_5_2Spans","商机产地长度应在2-30字以内","<img src='http://www.www.com/office/style/1/img/right.gif' border='0'>",false);
 idSpans[5] = new Array("item_5_4","^[\\d]+$","iditem_5_4Spans","商机数量是数字格式","<img src='http://www.www.com/office/style/1/img/right.gif' border='0'>",false);
 idSpans[6] = new Array("item_5_6","^[0-9]+[\\.]?[0-9]*$","iditem_5_6Spans","单价只能由数字和小数点组成!!","正确",false);
 idSpans[7] = new Array("item_7","^[\\s\\S]{16,}$","productContent","详细说明不能小于16个字","<img src='http://www.www.com/office/style/1/img/right.gif' border='0'>",true);
 idSpans[8] = new Array("item_8_2","^[\\s\\S]+$","iditem_8_2Spans","文件名称不能为空","<img src='http://www.www.com/office/style/1/img/right.gif' border='0'>",true);
 idSpans[9] = new Array("img_1_1","^[\\s\\S]+$","idimg_1Spans","图片不能为空","<img src='http://www.www.com/office/style/1/img/right.gif' border='0'>",true);


 var reg ;
 var textValue;
 for(var i=0;i<idSpans.length;i++)
 {
 
    reg = new RegExp(idSpans[i][1], "igm");
    textValue = document.getElementById(idSpans[i][0]).value;
    if(textValue.length>0||idSpans[i][5])
    {
  
        if(reg.test(textValue))
        {
       
            document.getElementById(idSpans[i][2]).innerHTML="<font color='green'>"+idSpans[i][4]+"</font>";
        }
        else
        {
            try
            {
            document.getElementById(idSpans[i][0]).focus();
            }
            catch(e)
            {
            alert(e.message);
            }
            document.getElementById(idSpans[i][2]).innerHTML="<font color='red'>"+idSpans[i][3]+"</font>";
        return false;
        }
    }
  
 }

你可能感兴趣的:(函数)