[PWEB-6] Validasi Form
Pada pertemuan keenam mata kuliah Pemrograman Web, kami mendapat tugas untuk membuat validasi form menggunakan HTML dan JavaScript. Berikut adalah code yang digunakan beserta outputnya.
Source code: GitHub
Output:
HTML code:
<!DOCTYPE html> <html lang="en"> <head> <title>Belajar Form Validation</title> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css"> <link rel="stylesheet" href="styles.css"> </head> <body> <div class="container-fluid d-flex align-items-center justify-content-center vh-100"> <div class="container"> <form name="formPendaftaran" action="daftar.php" method="post" onsubmit="return validateForm()"> <div class="form-group"> <label>Nama</label> <input type="text" name="nama" placeholder="Nama lengkap" class="form-control" required maxlength="40" minlength="3"> </div> <div class="form-group"> <label>Email</label> <input type="email" name="email" placeholder="Email Aktif" class="form-control"> <small id="emailHelp" class="form-text text-muted">Email Akan Digunakan Untuk Mengirimkan Notifikasi.</small> </div> <div class="form-group"> <label>Jurusan</label> <select name="jurusan" class="form-control"> <option value="0">Pilih Jurusan</option> <option value="1">Jurusan Informatika</option> <option value="2">Jurusan Teknik Komputer Jaringan</option> <option value="3">Jurusan Multimedia</option> </select> </div> <button type="submit" class="btn btn-primary">Submit</button> </form> </div> </div> <script> function validateForm() { if (document.forms["formPendaftaran"]["nama"].value == "") { alert("Nama Tidak Boleh Kosong"); document.forms["formPendaftaran"]["nama"].focus(); return false; } if (document.forms["formPendaftaran"]["email"].value == "") { alert("Email Tidak Boleh Kosong"); document.forms["formPendaftaran"]["email"].focus(); return false; } if (document.forms["formPendaftaran"]["jurusan"].selectedIndex < 1) { alert("Pilih Jurusan."); document.forms["formPendaftaran"]["jurusan"].focus(); return false; } } </script> </body> </html>
CSS code:
/* styles.css */ body { background-image: url('pic.jpg'); /* Replace 'path-to-your-image.jpg' with the actual path or URL of your image */ background-size: cover; /* Ensures the image covers the entire background */ background-repeat: no-repeat; /* Prevents the image from repeating */ background-attachment: fixed; /* Keeps the image fixed while scrolling */ background-position: center center; /* Centers the image horizontally and vertically */ } .container { max-width: 400px; padding: 20px; background-color: rgba(255, 255, 255, 0.8); /* Add a semi-transparent white background to the form container */ border-radius: 5px; box-shadow: 0 0 10px rgba(0, 0, 0, 0.1); } .container-fluid { height: 100%; display: flex; flex-direction: column; justify-content: center; align-items: center; } .form-group { margin-bottom: 15px; } label { font-weight: bold; } input[type="text"], input[type="email"], select { width: 100%; padding: 10px; border: 1px solid #ccc; border-radius: 4px; font-size: 16px; } select { height: 40px; } small.form-text { color: #888; } .btn-primary { background-color: #007bff; border: none; padding: 10px 20px; color: #fff; border-radius: 4px; cursor: pointer; } .btn-primary:hover { background-color: #0056b3; }
Reference:
https://belajarphp.net/tutorial-validasi-form-menggunakan-html-dan-javascript/



Komentar
Posting Komentar