From 4e0731a9c6e9baf9704e30d8cfde5b98dab9d5d3 Mon Sep 17 00:00:00 2001 From: amirhassan Date: Mon, 19 Feb 2024 12:19:54 +0800 Subject: [PATCH] Validate value < 0 and 2 decimal places for input berat permata --- resources/views/gadaian/v2/edit.blade.php | 59 +++++++++++++++++++++- resources/views/gadaian/v2/index.blade.php | 57 +++++++++++++++++++++ 2 files changed, 115 insertions(+), 1 deletion(-) diff --git a/resources/views/gadaian/v2/edit.blade.php b/resources/views/gadaian/v2/edit.blade.php index 75b0906..4995915 100644 --- a/resources/views/gadaian/v2/edit.blade.php +++ b/resources/views/gadaian/v2/edit.blade.php @@ -1031,6 +1031,19 @@ }); function validateBatuPermata(permata, berat) { + function countDecimalPlaces(value) { + const match = value.match(/(?:\.(\d+))?$/); + if (!match || !match[1]) { + return 0; + } + return match[1].length; + } + + function validateInput(inputValue) { + const decimalPlaces = countDecimalPlaces(inputValue); + return decimalPlaces <= 2; + } + if(permata != '') { if(berat == '') { $("#tambah_marhun").prop('disabled', false); @@ -1052,7 +1065,27 @@ }); event.preventDefault(); return false; - } else { + } else if(berat <= 0) { + $("#tambah_marhun").prop('disabled', false); + $("#terima_marhun").prop('disabled', false); + Swal.fire({ + icon: 'error', + title: 'Amaran!', + text: 'Nilai berat permata mesti lebih besar daripada ' + berat, + }); + event.preventDefault(); + return false; + } else if(!validateInput(berat)) { + $("#tambah_marhun").prop('disabled', false); + $("#terima_marhun").prop('disabled', false); + Swal.fire({ + icon: 'error', + title: 'Amaran!', + text: 'Hanya 2 titik perpuluhan dibenarkan untuk berat batu permata' + }); + event.preventDefault(); + return false; + }else { return true; } } else if(berat != '') { @@ -1068,6 +1101,30 @@ return false; } + if(berat <= 0) { + $("#tambah_marhun").prop('disabled', false); + $("#terima_marhun").prop('disabled', false); + Swal.fire({ + icon: 'error', + title: 'Amaran!', + text: 'Nilai berat permata ' + berat + ' tidak dibenarkan' + }); + event.preventDefault(); + return false; + } + + if(!validateInput(berat)) { + $("#tambah_marhun").prop('disabled', false); + $("#terima_marhun").prop('disabled', false); + Swal.fire({ + icon: 'error', + title: 'Amaran!', + text: 'Hanya 2 titik perpuluhan dibenarkan untuk berat batu permata' + }); + event.preventDefault(); + return false; + } + if(permata == '') { $("#tambah_marhun").prop('disabled', false); $("#terima_marhun").prop('disabled', false); diff --git a/resources/views/gadaian/v2/index.blade.php b/resources/views/gadaian/v2/index.blade.php index a217e5d..863a4cf 100644 --- a/resources/views/gadaian/v2/index.blade.php +++ b/resources/views/gadaian/v2/index.blade.php @@ -850,6 +850,19 @@ }); function validateBatuPermata(permata, berat) { + function countDecimalPlaces(value) { + const match = value.match(/(?:\.(\d+))?$/); + if (!match || !match[1]) { + return 0; + } + return match[1].length; + } + + function validateInput(inputValue) { + const decimalPlaces = countDecimalPlaces(inputValue); + return decimalPlaces <= 2; + } + if(permata != '') { if(berat == '') { $("#tambah_marhun").prop('disabled', false); @@ -871,6 +884,26 @@ }); event.preventDefault(); return false; + } else if(berat <= 0) { + $("#tambah_marhun").prop('disabled', false); + $("#terima_marhun").prop('disabled', false); + Swal.fire({ + icon: 'error', + title: 'Amaran!', + text: 'Nilai berat permata mesti lebih besar daripada ' + berat, + }); + event.preventDefault(); + return false; + } else if(!validateInput(berat)) { + $("#tambah_marhun").prop('disabled', false); + $("#terima_marhun").prop('disabled', false); + Swal.fire({ + icon: 'error', + title: 'Amaran!', + text: 'Hanya 2 titik perpuluhan dibenarkan untuk berat batu permata' + }); + event.preventDefault(); + return false; } else { return true; } @@ -887,6 +920,30 @@ return false; } + if(berat <= 0) { + $("#tambah_marhun").prop('disabled', false); + $("#terima_marhun").prop('disabled', false); + Swal.fire({ + icon: 'error', + title: 'Amaran!', + text: 'Nilai berat permata mesti lebih besar daripada ' + berat, + }); + event.preventDefault(); + return false; + } + + if(!validateInput(berat)) { + $("#tambah_marhun").prop('disabled', false); + $("#terima_marhun").prop('disabled', false); + Swal.fire({ + icon: 'error', + title: 'Amaran!', + text: 'Hanya 2 titik perpuluhan dibenarkan untuk berat batu permata' + }); + event.preventDefault(); + return false; + } + if(permata == '') { $("#tambah_marhun").prop('disabled', false); $("#terima_marhun").prop('disabled', false);