亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

javascript驗(yàn)證手機(jī)號(hào)和實(shí)現(xiàn)星號(hào)(*)代替實(shí)例

 更新時(shí)間:2016年08月16日 11:37:04   投稿:daisy  
在我們?nèi)粘i_發(fā)中經(jīng)常要驗(yàn)證客戶輸入的手機(jī)號(hào)是否正確,有的時(shí)候還需要將中間的四位或者前幾位用星號(hào)(*)代替,那該如何實(shí)現(xiàn)呢?下面跟著小編一起來看看。

一、JavaScript替換手機(jī)號(hào)中間4位

// 匹配手機(jī)號(hào)首尾,以類似“123****8901”的形式輸出
'12345678901'.replace(/(\d{3})\d{4}(\d{4})/, '$1****$2');

示例

<!doctype html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>無標(biāo)題文檔</title>
<script type="text/javascript">
var phone='12345678901';
var dh=phone.replace(/(\d{3})\d{4}(\d{4})/, '$1****$2');
alert (dh);
</script>
</head>

<body>
</body>
</html>

注意:此段正則匹配字符串中的連續(xù)11位數(shù)字,替換中間4位為*號(hào),輸出常見的隱匿手機(jī)號(hào)的格式。如果要僅得到末尾4位,則可以改成如下形式:

二、JavaScript替換手機(jī)號(hào)前7位

// 匹配連續(xù)11位數(shù)字,并替換其中的前7位為*號(hào)
'15110280327'.replace(/\d{7}(\d{4})/, '*******$1');

示例

<!doctype html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>無標(biāo)題文檔</title>
<script type="text/javascript">
var phone='12345678901';
var dh=phone.replace(/\d{7}(\d{4})/, '*******$1');
alert (dh);
</script>
</head>

<body>
</body>
</html>

補(bǔ)充注釋:正則表達(dá)式中的括號(hào)即可用于分組,同時(shí)也用于定義子模式串,在replace()方法中,參數(shù)二中可以使用$n(n為數(shù)字)來依次引用模式串中用括號(hào)定義的字串。

三、JavaScript手機(jī)驗(yàn)證以及隱藏手機(jī)號(hào)碼中間四位綜合實(shí)例

<!doctype html>
<html lang="en">
<head>
 <meta charset="UTF-8" />
 <title>js手機(jī)號(hào)碼驗(yàn)證以及隱藏中間四位數(shù)字</title>
 <script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
 <input type="text" id="myText">
 <p>js手機(jī)驗(yàn)證以及隱藏手機(jī)號(hào)碼中間四位</p>
 <input type="button" value="提交" id="subBtn">
 <script type='text/javascript'> 
 $(function(){
  $("#subBtn").click(function(){
  if($("#myText").val()==""){
  alert("手機(jī)號(hào)碼不能為空")
  }else{
  if(iphoneCheck(myText)){
  alert("提交成功");
  var phone=$("#myText").val();
  var myphone=phone.substr(3,4);
  //alert(myphone)
  var lphone=phone.replace(myphone,"****");
  $("#myText").val(lphone);
  }else{
  alert("請(qǐng)輸入正確的手機(jī)號(hào)碼")
  }
  }
  
  function iphoneCheck(id){
  var temp=document.getElementById("myText");
  var re=/^[1][34587]\d{9}$/;//手機(jī)號(hào)碼驗(yàn)證正則表達(dá)式
  if(re.test(temp.value)){
  return true;
  }else{
  return false;
  }
  }
  });
 });
 </script>
</body>
</html>

總結(jié)

以上就是javascript驗(yàn)證手機(jī)號(hào)與實(shí)現(xiàn)星號(hào)(*)代替效果的全部內(nèi)容,希望本文的內(nèi)容對(duì)大家日常使用JavaScript能有所幫助。

相關(guān)文章

最新評(píng)論