JAvascriptマッチングURL、E-mail、電話番号、携帯電話番号
4374 ワード
/**
*
*
* @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" onchange="check_regex('description_area','description_area_msg','<?php echo $jsregister_messsage['you_cant_input'];?>');" cols="45" rows="5"><?php echo @$_POST['description_area'];?></textarea>