feat: inital commit project
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
/* phone.js — Normalisasi & validasi nomor WhatsApp Indonesia */
|
||||
(function (global) {
|
||||
'use strict';
|
||||
|
||||
function normalize(raw) {
|
||||
if (!raw) return '';
|
||||
var s = String(raw).replace(/[^\d+]/g, '');
|
||||
if (s.charAt(0) === '+') s = s.slice(1);
|
||||
if (s.indexOf('620') === 0) s = '62' + s.slice(3);
|
||||
if (s.charAt(0) === '0') s = '62' + s.slice(1);
|
||||
else if (s.charAt(0) === '8') s = '62' + s;
|
||||
return s;
|
||||
}
|
||||
|
||||
function isValid(norm) {
|
||||
return /^62\d{8,13}$/.test(norm || '');
|
||||
}
|
||||
|
||||
function formatDisplay(norm) {
|
||||
if (!norm) return '';
|
||||
if (norm.indexOf('62') !== 0) return '+' + norm;
|
||||
var rest = norm.slice(2);
|
||||
var groups = rest.match(/.{1,4}/g) || [rest];
|
||||
return '+62 ' + groups.join('-');
|
||||
}
|
||||
|
||||
global.Phone = { normalize: normalize, isValid: isValid, formatDisplay: formatDisplay };
|
||||
})(window);
|
||||
Reference in New Issue
Block a user