-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
66 lines (62 loc) · 2.58 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/**=====================================================================
https://code4mk.org
https://github.com/code4mk
==========================================================================*/
let bdPhone = function phone(num) {
// operator number <num>
if (num !== undefined && num.trim() !== '') {
const validatorRegex = /(\+){0,1}(88){0,1}01(3|5|6|7|8|9)(\d){8}/;
var originalNum = num;
let countryCode = '+88';
let status = {};
let collectDigit = originalNum.replace(/[\D]/gi,'');
let reduntantCountryCode = countryCode.concat(collectDigit.replace(/^\+*8+/gi,''));
let bdSuggestPhone = reduntantCountryCode.slice(0,14);
originalNum.slice(0,3) === '+88' ? status['input_length'] = 14 : status['input_length'] = 11
if (validatorRegex.exec(originalNum) !== null && validatorRegex.exec(originalNum)[0] === originalNum ) {
status['core_valid'] = true
} else {
status['core_valid'] = false
}
function checkOperatorDigit() {
let operatorDigit = bdSuggestPhone.slice(5,6)
if ( operatorDigit === '3'||operatorDigit === '5'||operatorDigit === '6'||operatorDigit === '7'||operatorDigit === '8'||operatorDigit === '9') {
return true
}
return false
}
// operator status & need digit
if (validatorRegex.exec(bdSuggestPhone) === null ) {
if (checkOperatorDigit() && bdSuggestPhone.length < 14 ) {
status['digit_status'] = 'Number length must be 11 digits'
status['need_digit'] = 14 - bdSuggestPhone.length
} else {
status['has_operator'] = false
status['operator_status'] = `(${bdSuggestPhone.slice(3,6)}) invalid operator`
status['status'] = `(${bdSuggestPhone.slice(3,6)}) invalid operator`
}
} else {
status['has_operator'] = true
status['operator_status'] = `(${bdSuggestPhone.slice(3,6)}) valid operator`
if (bdSuggestPhone.length >= 14) {
status['suggest_num'] = bdSuggestPhone
status['status'] = bdSuggestPhone
}
// digit status exceed
if (reduntantCountryCode.length > 14){
countExceedDigit = reduntantCountryCode.length - 14
status['exceed'] = countExceedDigit
status['has_exceed'] = true
status['original_number'] = reduntantCountryCode
status['exceed_digit'] = reduntantCountryCode.slice(14,reduntantCountryCode.length)
status['digit_status'] = `exceed ${countExceedDigit} digit`
}
}
// console.log(status)
return status
} else {
console.log('phone is undefined')
}
}
module.exports = bdPhone
module.exports.default = bdPhone;