From dc2e4033491636a6526db9a29a27ae295ae0732e Mon Sep 17 00:00:00 2001
From: Iqbal <iqbalkhowarizmi@ymail.com>
Date: Wed, 4 Oct 2017 20:17:11 +0700
Subject: [PATCH] Added username and email validation. Still using mock data
 though.

---
 src/model/emailList.php    | 38 ++++++++++++++++
 src/model/usernameList.php | 32 +++++++++++++
 src/view/register.html     | 93 ++++++++++++++++++++++++++++++++++++++
 src/view/script.js         | 24 ++++++++++
 4 files changed, 187 insertions(+)
 create mode 100644 src/model/emailList.php
 create mode 100644 src/model/usernameList.php
 create mode 100644 src/view/register.html
 create mode 100644 src/view/script.js

diff --git a/src/model/emailList.php b/src/model/emailList.php
new file mode 100644
index 0000000..a7e7bcf
--- /dev/null
+++ b/src/model/emailList.php
@@ -0,0 +1,38 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: iqbal
+ * Date: 03/10/17
+ * Time: 23:43
+ */
+
+$emailList = array("iqbal@gmail.com", "test@ymail.com", "cloud@dagojek.com");
+
+$emailInput = $_REQUEST["q"];
+
+if (filter_var($emailInput, FILTER_VALIDATE_EMAIL)) {
+    $response = checkAvailability($emailInput, $emailList);
+} else {
+    $response = "unavailable";
+}
+
+echo $response;
+
+function checkAvailability($string, $aList) {
+    if ($string !== "") {
+        $found = false;
+        $i = 0;
+        while ($i < count($aList) && !$found) {
+            if ($string === $aList[$i]) {
+                $found = true;
+            } else {
+                $i++;
+            }
+        }
+        if ($found) {
+            return "unavailable";
+        } else {
+            return "available";
+        }
+    }
+}
\ No newline at end of file
diff --git a/src/model/usernameList.php b/src/model/usernameList.php
new file mode 100644
index 0000000..9b76f88
--- /dev/null
+++ b/src/model/usernameList.php
@@ -0,0 +1,32 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: iqbal
+ * Date: 03/10/17
+ * Time: 23:43
+ */
+
+$usernameList = array("iqbal", "test", "cloud");
+
+$usernameInput = $_REQUEST["q"];
+$response = checkAvailability($usernameInput, $usernameList);
+echo $response;
+
+function checkAvailability($string, $aList) {
+    if ($string !== "") {
+        $found = false;
+        $i = 0;
+        while ($i < count($aList) && !$found) {
+            if ($string === $aList[$i]) {
+                $found = true;
+            } else {
+                $i++;
+            }
+        }
+        if ($found) {
+            return "unavailable";
+        } else {
+            return "available";
+        }
+    }
+}
\ No newline at end of file
diff --git a/src/view/register.html b/src/view/register.html
new file mode 100644
index 0000000..00b15d4
--- /dev/null
+++ b/src/view/register.html
@@ -0,0 +1,93 @@
+<!DOCTYPE html>
+<html lang="en">
+<head>
+    <meta charset="UTF-8">
+    <title>Register Page | Dagojek</title>
+    <link rel="stylesheet" type="text/css" href="style.css">
+</head>
+
+<body id="register-body">
+
+<div class="container">
+
+    <header id="register-header">
+        <h1>
+            Dagojek Register<br>
+            <small>Create New Account</small>
+        </h1>
+    </header>
+
+    <section id="register-main">
+        <div id="register-panel">
+            <form>
+                <fieldset>
+                    <legend>Sign Up</legend>
+                    <table>
+                        <tr>
+                            <td>
+                                Name:
+                            </td>
+                            <td>
+                                <input type="text" placeholder="Your name" name="name">
+                            </td>
+                        </tr>
+                        <tr>
+                            <td>
+                                Username:
+                            </td>
+                            <td>
+                                <input onkeyup="checkAvailability(this.value, 'username', '../model/usernameList.php')" id="username" type="text" placeholder="Username" name="username">
+                            </td>
+                        </tr>
+                        <tr>
+                            <td>
+                                Email:
+                            </td>
+                            <td>
+                                <input onkeyup="checkAvailability(this.value, 'email', '../model/emailList.php')" id="email" type="email" placeholder="Email address" name="email">
+                            </td>
+                        </tr>
+                        <tr>
+                            <td>
+                                Password:
+                            </td>
+                            <td>
+                                <input type="password" placeholder="Password" name="password">
+                            </td>
+                        </tr>
+                        <tr>
+                            <td>
+                                Confirm Password:
+                            </td>
+                            <td>
+                                <input type="password" placeholder="Confirm password" name="confirm-password">
+                            </td>
+                        </tr>
+                        <tr>
+                            <td>
+                                Phone Number:
+                            </td>
+                            <td>
+                                <input type="number" placeholder="Phone number" name="phone-number">
+                            </td>
+                        </tr>
+                    </table>
+                    <input type="checkbox" value="is-driver"> I want to be a driver as well!
+                    <br>
+                    <input type="submit" value="Login">
+                    <br>
+                </fieldset>
+            </form>
+        </div>
+    </section>
+
+    <footer>
+
+    </footer>
+
+</div>
+
+<!-- Script -->
+<script type="application/javascript" src="script.js"></script>
+</body>
+</html>
\ No newline at end of file
diff --git a/src/view/script.js b/src/view/script.js
new file mode 100644
index 0000000..56adad5
--- /dev/null
+++ b/src/view/script.js
@@ -0,0 +1,24 @@
+function checkAvailability(string, elmtID, dataCollection) {
+    var field = document.getElementById(elmtID)
+
+    if (string.length === 0) {
+        field.classList.remove("available");
+        field.classList.remove("unavailable");
+        return;
+    } else {
+        var xmlhttp = new XMLHttpRequest();
+        xmlhttp.onreadystatechange = function () {
+            if (this.readyState === 4 && this.status === 200) {
+                if (this.responseText === "available") {
+                    field.classList.remove("unavailable");
+                    field.classList.add("available");
+                } else {
+                    field.classList.remove("available");
+                    field.classList.add("unavailable");
+                }
+            }
+        };
+        xmlhttp.open("GET", dataCollection + "?q=" + string, true);
+        xmlhttp.send();
+    }
+}
\ No newline at end of file
-- 
GitLab