Let us learn to make your own website using the programming language php, java, html, css etc.

Validasi Input Hanya Angka

by dekurnia , at 9:06 AM , have 0 comments
  • Input Hanya Angka

    Kasus yang cocok misalnya dalah input No Telp/Hp pada form, ok tanpa tunggu lama-lama :Dlangsung buat form nya, kalo males, ni kode form nya

    <form method="post" action="aksi.php" onsubmit="return validasi_input(this)">
    <p>Telp/Hp : <input name="telp" type="text"></p>
    <p><input name="" type="submit" value="Submit"></p>
    </form>
    
    
    Kalo di lihat, nama form input tersebut adalah telp, jadi value dari form telp tersebut yang harus di cek satu per satu jika bukan angka langsung di tampilkan isi dari alertnya. Berikut kode javascriptnya.

    <script type="text/javascript">
      function validasi_input(form){
      if (form.telp.value != ""){
      var x = (form.telp.value);
      var status = true;
      var list = new Array("0", "1", "2", "3", "4", "5", "6", "7", "8", "9");
      for (i=0; i<=x.length-1; i++)
      {
      if (x[i] in list) cek = true;
      else cek = false;
     status = status && cek;
      }
      if (status == false)
      {
      alert("Telp harus angka!");
      form.telp.focus();
      return false;
      }
      }
      return (true);
      }
      </script>

dekurnia
Validasi Input Hanya Angka - written by dekurnia , published at 9:06 AM, categorized as Java Script . And have 0 comments
No comment Add a comment
Cancel Reply
GetID
Theme designed by Damzaky - Published by Proyek-Template
Powered by Blogger