从京东的订单流程中摘抄下来JS表单验证函数:
/** * <strong>判断是否是空</strong> * @param value */ function isEmpty(value){ if(value == null || value == "" || value == "undefined" || value == undefined || value == "null"){ return true; } else{ value = value.replace(/\s/g,""); if(value == ""){ return true; } return false; } }
/** * <strong>判断是否是数字</strong> */ function isNumber(value){ if(isNaN(value)){ return false; } else{ return true; } }
/** * <strong>只包含中文和英文</strong> * @param cs * @returns {Boolean} */ function isGbOrEn(value){ var regu = "^[a-zA-Z\u4e00-\u9fa5]+$"; var re = new RegExp(regu); if (value.search(re) != -1){ return true; } else { return false; } }
/** * 检查邮箱格式 * @param email * @returns {Boolean} */ function check_email(email){ if(email){ var myReg=/(^\s*)\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*(\s*$)/; if(!myReg.test(email)){return false;} return true; } return false; }/** * <strong>检查手机号码</strong> * @param mobile * @returns {Boolean} */ function check_mobile(mobile){ var regu = /^\d{11}$/; var re = new RegExp(regu); if(!re.test(mobile)){ return false; } return true; }/** * <strong>验证电话号码,带"(,),-"字符和数字其他不通过</strong> * @param str * @returns {Boolean} */ function checkPhone(str){ if(str.length > 20){ return false; } var patternStr = "(0123456789-)"; var strlength=str.length; for(var i=0;i<strlength;i++){ var tempchar=str.substring(i,i+1); if(patternStr.indexOf(tempchar)<0){ return false; } } return true ; }/** * <strong>验证银行账户,带"(, ),-"字符和数字其他不通过</strong> * @param str * @returns {Boolean} */ function checkBankCount(str){ if(str.length > 50){ return false; } var patternStr = "(0123456789- )"; var strlength=str.length; for(var i=0;i<strlength;i++){ var tempchar=str.substring(i,i+1); if(patternStr.indexOf(tempchar)<0){ return false; } } return true ; }<strong>//正则</strong> function trimTxt(txt){ return txt.replace(/(^\s*)|(\s*$)/g, ""); }/** * <strong>检查是否含有非法字符</strong> * @param temp_str * @returns {Boolean} */ function is_forbid(temp_str){ temp_str=trimTxt(temp_str); temp_str = temp_str.replace('*',"@"); temp_str = temp_str.replace('--',"@"); temp_str = temp_str.replace('/',"@"); temp_str = temp_str.replace('+',"@"); temp_str = temp_str.replace('\'',"@"); temp_str = temp_str.replace('\\',"@"); temp_str = temp_str.replace('$',"@"); temp_str = temp_str.replace('^',"@"); temp_str = temp_str.replace('.',"@"); temp_str = temp_str.replace(';',"@"); temp_str = temp_str.replace('<',"@"); temp_str = temp_str.replace('>',"@"); temp_str = temp_str.replace('"',"@"); temp_str = temp_str.replace('=',"@"); temp_str = temp_str.replace('{',"@"); temp_str = temp_str.replace('}',"@"); var forbid_str=new String('@,%,~,&'); var forbid_array=new Array(); forbid_array=forbid_str.split(','); for(i=0;i<forbid_array.length;i++){ if(temp_str.search(new RegExp(forbid_array[i])) != -1) return false; } return true; }/** * <strong>检查数量</strong> * @param txtObj * @returns {Number} */ function checkLength(txtObj){ var val=txtObj; var valLength=0; for(var ii=0;ii<val.length;ii++){ var word=val.substring(ii,1); if(/[^\x00-\xff]/g.test(word)){ valLength+=2; }else{ valLength++; } } return valLength; }
第一部手机moto v600“`的泪目了啊