Minggu, 15 Januari 2017

Validasi dalam PHP


Langsung saja kodingannya seperti berikut:


database.php
<?php
include('simpan_data.php');
$host = "localhost";
$user = "root";
$password = "";
$name = "p8";
$connect = mysql_connect($host, $username, $password) or die("Koneksi ke database gagal!");
mysql_select_db($name, $connect) or die("Tidak ada database yang dipilih!");

?>


simpan_data.php
<?php
if(isset($_POST['submit'])){
include "database.php";
$username = $_POST['username'];
$password = $_POST['password'];
$email = $_POST['email'];
$tgllhr = $_POST['tgllhr'];
$jk = $_POST['jk'];
$input = mysql_query("INSERT INTO form VALUES(NULL, '$username', '$password', '$email', '$tgllhr', '$jk')") or die(mysql_error());

if($input){
echo '<center><h1>Data berhasil di tambahkan! ';
echo '<a href="validasi.php"><h1>Kembali</h1></a>';
}else{
echo '<h1>Gagal menambahkan data! </h1>';
echo '<a href="validasi.php"><h1>Kembali</a></h1>';
}
}

?>
validasi.php
<html>
<head><title>form postest </title></head>
<body>
<form name="form" action="simpan_data.php" method="POST" onSubmit="return validasi()">
<table border="2" align="center" height="1" width="20" cellpadding="1">
<td>
<b><font color="black" size="6"> Form mahasiswa</font></b>
<p>
<input type="text" id="username" name="username" size="50" placeholder=" username anda ">
<br><br><br>
<input type="password" id="password" name="password" size="50" placeholder=" password anda ">
<br><br><br>
<input type="text" id="email" name="email" size="50" placeholder=" email anda ">
<br><br><br>
<input type="text" id="tgl" name="tgllhr" size="50" placeholder=" tanggal lahir anda">
<br><br>
<h3><p><b> Jenis Kelamin : </h3>
<h4><input type="radio" name="jk" value="Laki Laki" id="pria">Laki Laki</h4>
<h4><input type="radio" name="jk" value="Perempuan" id="wanita">Perempuan</h4>
<input type="submit" value="Submit" id="Submit" name="submit" onClick="validasi()">
</table>
</form>
<script type="text/javascript">
function validasi(){
var numbers=/^[0-9]+$/;
var huruf=/^[a-zA-Z ]+$/;
var email = document.getElementById('email').value;
pola_email=/^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
var username = document.getElementById('username').value;
var password = document.getElementById('password').value;
var pria=form.jk[0].checked;
var wanita=form.jk[1].checked;
var validtanggal=/^(0[1-9]|[12][0-9]|3[01])\-(0[1-9]|1[012])\-[0-9]{4}/;
var tgl=document.getElementById('tgl').value;
if (username==null || username==""){
        alert("Username harus di isi !!!");
       }else if (username.length <6 || username.length>19){
        alert ("Username Harus Antara 6 sampai 19 karakter !!!");
       }
       if (password==null || password==""){
        alert("Password harus di isi");
        return false;
       }else if (password.length<6 || password.length>19){
        alert ("Password Harus Antara 6 sampai 19 karakter !!!");
       }else if (password==username){
        alert ("Password dan Username harus beda !!!");
       }
       if (email==null || email==""){
        alert("Email harus di isi !!!");
       }else if (email.length<5){
        alert ("Panjang Email Harus Lebih Dari 4 Karakter !!! ")
       }else if (!pola_email.test(email)){
        alert ("Format Email yang Anda Masukkan Salah !!!");
        return false;
       }
       if (!tgl.match(validtanggal)) {
        alert ("Format Tanggal Harus dd-mm-yyyy ");
        ("document.getElementById").focus(tgl);
        return false;
       }
       if (pria==false && wanita==false){
        alert ("Pilih Jenis Kelamin anda !!!");
       }
   }
</script>
</body>
</html>

Nama databasenya "p8"
tabelnya "form"

Tidak ada komentar:

Posting Komentar