<html>
<head>
  <title>Contoh Validasi Saldo</title>
  <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body>
  <form id="form" onsubmit="return validasiForm()">
    <input type="hidden" name="tsaldo" value="50000" id="tsaldoHidden">
    <label>Saldo: Rp. <span id="tsaldo">50.000,00</span></label><br>
    <label>Penarikan:</label>
    <input type="text" name="ttarik" id="ttarik" required onkeyup="formatRupiahDuaDigit(this)"><br>
    <input type="hidden" name="ttarikHidden" id="ttarikHidden">
    <button type="submit">Submit</button>
  </form>

  <script>
function formatRupiahDuaDigit(obj) {
  var nilai = obj.value.replace(/\./g, '');
  if (nilai.indexOf(',') != -1) {
    var parts = nilai.split(',');
    var integerPart = addCommas(parts[0]);
    obj.value = integerPart + ',' + parts[1];
  } else {
    obj.value = addCommas(nilai);
  }
  document.getElementById('ttarikHidden').value = obj.value.replace(/\./g, '').replace(',', '.');
}

function addCommas(nStr) {
  nStr += '';
  var rgx = /(\d+)(\d{3})/;
  while (rgx.test(nStr)) {
    nStr = nStr.replace(rgx, '$1' + '.' + '$2');
  }
  return nStr;
}


    function validasiForm() {
      var saldo = parseInt(document.getElementById('tsaldoHidden').value);
      var tarik = parseFloat(document.getElementById('ttarikHidden').value);
      if (isNaN(tarik)) {
        alert('Input tidak valid');
        return false;
      }
      if (tarik > saldo) {
        alert('SALDO KURANG');
        return false;
      }
      return true;
    }
  </script>
</body>
</html>