About a week ago, I came to Stack Overflow to look for a way to validate a Kenyan phone number in JavaScript. I sadly browsed through questions that were about other countries` numbers. So yesterday I succeeded in creating it. I thought its a good idea to share it with you. Especially for Hybrid App (Cordova) and Web developers.
大约一周前,我来到Stack Overflow寻找一种方法来验证JavaScript中的肯尼亚电话号码。我遗憾地浏览了有关其他国家号码的问题。所以昨天我成功地创造了它。我认为与你分享是一个好主意。特别适用于Hybrid App(Cordova)和Web开发人员。
1 个解决方案
#1
0
I have used jQuery and HTML. It's good to note that Kenyan phone numbers have a 07## ### ### format. It is meant for developers working with JavaScript in delivering custom services to Kenyan users.
我使用过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>
#1
0
I have used jQuery and HTML. It's good to note that Kenyan phone numbers have a 07## ### ### format. It is meant for developers working with JavaScript in delivering custom services to Kenyan users.
我使用过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>