2017-05-24 203 views
0

大约一个星期前,我来到Stack Overflow寻找一种在JavaScript中验证肯尼亚电话号码的方法。我可悲地浏览了一些关于其他国家数字的问题。所以昨天我成功地创造了它。我认为这是一个好主意,与你分享。特别是对于混合应用程序(Cordova)和Web开发人员。肯尼亚的正则表达式电话号码格式

+0

你问了个问题吗? – Whothehellisthat

+0

我投票结束这个问题作为题外话,因为这是一个没有问题的答案(对不起!) – halfer

+0

您应该将答案部分移到答案部分。 – Dijkgraaf

回答

0

我已经使用jQuery和HTML。值得注意的是,肯尼亚的电话号码有07 ## ### ###格式。它适用于使用JavaScript为肯尼亚用户提供定制服务的开发人员。

<script type="text/javascript"> 
$(document).ready(function(){ 
    $("#verify").click(function(){ 

     var phoneNumber = $("#phoneNumber").val(); 
     var toMatch = /^07\d{8}$/; 
     /* 
     *Assumming that you have added a script tag to refer to the jQuery library in your HTML head tags 
     -for example <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></closing script tag> 
     *Assuming you have an HTML input with an id="phoneNumber" 
     *Assuming you have a HTML button with an id="verify" 
     *Assuming you have a HTML paragraph with an id="result" 
     *^means matches at the beginning of the line 
     * $ means matches at the end of the line 
     * \ means backslash escape to make a normal character special 
     * /d means a single digit character 
     * {n} mean the n occurence of the preceding match 
     *the two forward slashes that start and end are basically what enclose our REGEXP 
     *var toMatch = \our pattern\; is the syntax as briefly mentioned in previous comment 
     */ 
     var authenticate = toMatch.test(phoneNumber); 
     if (authenticate) { 
      $("#phoneNumber").focus(); 
      $("#result").html("Valid Kenyan Number"); 

     }else{ 
      $("#phoneNumber").focus(); 
      $("#result").html("Invalid Kenyan Number"); 
     } 

    }); 
}); 
</script>