first commit
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
/**
|
||||
* Composable untuk menyediakan fungsi dan aturan validasi umum.
|
||||
*/
|
||||
export const useValidation = () => {
|
||||
|
||||
const required = (value, message = 'Bidang ini wajib diisi.') => {
|
||||
// Memastikan nilai ada dan bukan string kosong (setelah trim)
|
||||
return (value !== null && value !== undefined && String(value).trim() !== '') || message;
|
||||
};
|
||||
|
||||
const minNameLength = (value, minLength = 3) => {
|
||||
if (!value) return true; // Lewati jika kosong (handle oleh 'required')
|
||||
const length = value.trim().length; // Hitung panjang setelah menghapus spasi di awal/akhir
|
||||
return length >= minLength || `Panjang minimal adalah ${minLength} karakter.`;
|
||||
};
|
||||
|
||||
const isNumber = (value) => {
|
||||
if (!value) return true; // Lewati jika kosong (handle oleh 'required')
|
||||
return /^[0-9]+$/.test(value) || 'Hanya boleh mengandung angka.';
|
||||
};
|
||||
|
||||
const phoneLength = (value) => {
|
||||
if (!value) return true;
|
||||
// Hapus karakter non-angka sebelum menghitung panjang
|
||||
const length = value.replace(/[^0-9]/g, '').length;
|
||||
return (length >= 9 && length <= 15) || 'Nomor telepon harus antara 9 hingga 15 digit.';
|
||||
};
|
||||
|
||||
const indonesianPhoneFormat = (value) => {
|
||||
if (!value) return true;
|
||||
// Pola: Harus dimulai dengan '08', diikuti 7 hingga 13 digit angka
|
||||
return /^08[0-9]{7,13}$/.test(value) || 'Format nomor telepon tidak valid (Contoh: 08xxxxxxxx).';
|
||||
};
|
||||
|
||||
// Gabungkan semua aturan validasi telepon ke dalam satu objek untuk kemudahan
|
||||
const phoneRules = [
|
||||
required,
|
||||
isNumber,
|
||||
phoneLength,
|
||||
indonesianPhoneFormat // Opsional: Hapus komentar jika format 08 wajib
|
||||
];
|
||||
|
||||
const textRules = [
|
||||
required,
|
||||
minNameLength
|
||||
];
|
||||
|
||||
const requiredRules = [
|
||||
required
|
||||
]
|
||||
|
||||
return {
|
||||
required,
|
||||
isNumber,
|
||||
phoneLength,
|
||||
indonesianPhoneFormat,
|
||||
phoneRules,
|
||||
textRules,
|
||||
requiredRules
|
||||
};
|
||||
};
|
||||
Reference in New Issue
Block a user