Jumat, 13 Maret 2015

Validasi Form dengan javascipt

</div>
<pre class="html" name="code"><style type="text/css">
#content .error {
border: 1px solid red;
}
#content .normal {
border: 1px solid #b6b5b4;
}
</style>
<script language="javascript">
function cekTambah() {
if (document.getElementById("cbKategori").value == "") {
alert("Kategori balum dipilih");
document.getElementById("cbKategori").focus();
document.getElementById("cbKategori").className = "error";
return false;
} else {
document.getElementById("cbKategori").className = "normal";
}
if (document.getElementById("txtNamaProduk").value == "") {
alert("nama produk harus disi");
document.getElementById("txtNamaProduk").focus();
document.getElementById("txtNamaProduk").className = "error";
return false;
} else {
document.getElementById("txtNamaProduk").className = "normal";
}
if (document.getElementById("txtHarga").value == "") {
alert("harga produk harus disi");
document.getElementById("txtHarga").focus();
document.getElementById("txtHarga").className = "error";
return false;
} else {
document.getElementById("txtHarga").className = "normal";
}
if (document.getElementById("txtStok").value == "") {
alert("stok produk harus disi");
document.getElementById("txtStok").focus();
document.getElementById("txtStok").className = "error";
return false;
} else {
document.getElementById("txtStok").className = "normal";
}
if (document.getElementById("txtDeskripsi").value == "") {
alert("deskripsi produk harus disi");
document.getElementById("txtDeskripsi").focus();
document.getElementById("txtDeskripsi").className = "error";
return false;
} else {
document.getElementById("txtDeskripsi").className = "normal";
}
if (document.getElementById("fupload").value == "") {
alert("deskripsi produk harus disi");
document.getElementById("fupload").focus();
document.getElementById("fupload").className = "error";
return false;
} else {
document.getElementById("fupload").className = "normal";
}
}
</script>
<h2 id="x">
Tambah Produk</h2>
<form action="?page=produk.proses_tambah_produk" enctype="multipart/form-data" method="post"
onsubmit="return cekTambah()">
<table class="data"> <tbody>
<tr> <td width="30%">Kategori</td> <td><select id="cbKategori" name="cbKategori">
<option value="">- pilih -</option>
<option value="kategori_a">-Kategori A-</option>
<option value="kategori_b">-Kategori B-</option>
</option> </select> </td> </tr>
<tr> <td>Nama Produk</td> <td><input id="txtNamaProduk" name="txtNamaProduk" type="text"/></td> </tr>
<tr> <td>Harga</td> <td><input id="txtHarga" name="txtHarga" type="text"/></td> </tr>
<tr> <td>Stok</td> <td><input id="txtStok" name="txtStok" type="text"/></td> </tr>
<tr> <td>Deskripsi</td> <td><textarea id="txtDeskripsi" name="txtDeskripsi"></textarea></td> </tr>
<tr> <td>Image</td> <td><input id="fupload" name="fupload" size="30" type="file"/></td> </tr>
<tr> <td></td> <td><input type="submit" value="Tambah"/> </td> </tr>
</tbody></table>
</form>
</pre>
</div>
view raw index.php hosted with ❤ by GitHub

Share: