/** * 检测国际电话 * * @param inputId * <input>的ID * @param inputMsgId * <span>的id,做为存放<input>的消息 * @param msg * 提示消息,提示如:“您不能输入+44-7778-188 810”,其中电话号码是正则提取的。 * @return boolean */ function check_telephone(inputId, inputMsgId, msg) { var input = document.getElementById(inputId); var inputMsg = document.getElementById(inputMsgId); regex_telephone = /((?:\(?[0\+]\d{2,3}\)?)[\s-]?(?:(?:\(0{1,3}\))?[0\d]{1,4})[\s-](?:[\d]{7,8}|[\d]{3,4}[\s-][\d]{3,4}))/; if (regex_telephone.test(input.value) == true) { if (inputMsg != null) { inputMsg.innerHTML = msg + RegExp.$1; } return true; } else { if (inputMsg != null) { inputMsg.innerHTML = ""; } return false; } } /** * 检测国际手机号码 * @param inputId <input>的ID * @param inputMsgId <span>的id,做为存放<input>的消息 * @param msg 提示消息,提示如:“您不能输入+1-626-2287211”,其中手机号码是正则提取的。 * @return boolean */ function check_mobile(inputId, inputMsgId, msg) { var input = document.getElementById(inputId); var inputMsg = document.getElementById(inputMsgId); regex_telephone = /((?:\(?[0\+]?\d{1,3}\)?)[\s-]?(?:0|\d{1,4})[\s-]?(?:(?:13\d{9})|(?:\d{7,8})))/; if (regex_telephone.test(input.value) == true) { if (inputMsg != null) { inputMsg.innerHTML = msg + RegExp.$1; } return true; } else { if (inputMsg != null) { inputMsg.innerHTML = ""; } return false; } } /** * 检查E-mail * @param inputId <input>的ID * @param inputMsgId <span>的id,做为存放<input>的消息 * @param msg 提示消息,提示如:“您不能输入+1-626-2287211”,其中手机号码是正则提取的。 * @return boolean */ function check_email(inputId, inputMsgId, msg) { var input = document.getElementById(inputId); var inputMsg = document.getElementById(inputMsgId); regex_emai = /([\w][\w-\.]*@[\w][\w-_]*\.[\w][\w\.]+)/; if (regex_emai.test(input.value) == true) { if (inputMsg != null) { inputMsg.innerHTML = msg + RegExp.$1; } return true; } else { if (inputMsg != null) { inputMsg.innerHTML = ""; } return false; } } /** * 检查URL * @param inputId <input>的ID * @param inputMsgId <span>的id,做为存放<input>的消息 * @param msg 提示消息,提示如:“您不能输入+1-626-2287211”,其中手机号码是正则提取的。 * @return boolean */ function check_url(inputId, inputMsgId, msg) { var input = document.getElementById(inputId); var inputMsg = document.getElementById(inputMsgId); regex_url = /((?:\w{3,5}:\/\/)?(\w)+\.(\w)+\.(\w)+(?:\/?.*))/; if (regex_url.test(input.value) == true) { if (inputMsg != null) { inputMsg.innerHTML = msg + RegExp.$1; } return true; } else { if (inputMsg != null) { inputMsg.innerHTML = ""; } return false; } }
全部调用
function check_regex(inputId, inputMsgId, msg){ var input = document.getElementById(inputId); if(check_url(inputId,inputMsgId,msg)==true){ input.style.backgroundColor="#FF9F9F"; return false; }else if(check_telephone(inputId,inputMsgId,msg)==true){ input.style.backgroundColor="#FF9F9F"; return false; }else if(check_mobile(inputId,inputMsgId,msg)==true){ input.style.backgroundColor="#FF9F9F"; return false; }else if(check_email(inputId,inputMsgId,msg)==true){ input.style.backgroundColor="#FF9F9F"; return false; }else{ input.style.backgroundColor="#B8F5B1"; } }
<textarea name="description_area" id="description_area" οnchange="check_regex('description_area','description_area_msg','<?php echo $jsregister_messsage['you_cant_input'];?>');" cols="45" rows="5"><?php echo @$_POST['description_area'];?></textarea>