From 70ba879e474a6784ee1f105f8861dd7b889a5848 Mon Sep 17 00:00:00 2001
From: Iqbal <iqbalkhowarizmi@ymail.com>
Date: Thu, 5 Oct 2017 23:24:53 +0700
Subject: [PATCH] Added email and username validation functionality.

---
 src/controller/EmailValidationController.php  | 25 +++++++++++++++++++
 .../UsernameValidationController.php          | 21 ++++++++++++++++
 src/view/register.html                        |  4 +--
 src/view/script.js                            | 13 ++++++++--
 4 files changed, 59 insertions(+), 4 deletions(-)
 create mode 100644 src/controller/EmailValidationController.php
 create mode 100644 src/controller/UsernameValidationController.php

diff --git a/src/controller/EmailValidationController.php b/src/controller/EmailValidationController.php
new file mode 100644
index 0000000..b8397ad
--- /dev/null
+++ b/src/controller/EmailValidationController.php
@@ -0,0 +1,25 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: iqbal
+ * Date: 05/10/17
+ * Time: 23:14
+ */
+
+    include_once "../controller/Controller.php";
+    include_once "../model/User.php";
+
+    $emailInput = $_REQUEST['q'];
+
+    $pdo = DB::getInstance();
+    if ($emailInput !== "") {
+        if (filter_var($emailInput, FILTER_VALIDATE_EMAIL)) {
+            if (User::GetUserBy("email", $emailInput, $pdo)) {
+                echo "unavailable";
+            } else {
+                echo "available";
+            }
+        } else {
+            echo "unavailable";
+        }
+    }
\ No newline at end of file
diff --git a/src/controller/UsernameValidationController.php b/src/controller/UsernameValidationController.php
new file mode 100644
index 0000000..377fb11
--- /dev/null
+++ b/src/controller/UsernameValidationController.php
@@ -0,0 +1,21 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: iqbal
+ * Date: 05/10/17
+ * Time: 23:12
+ */
+
+    include_once "../controller/Controller.php";
+    include_once "../model/User.php";
+
+    $usernameInput = $_REQUEST['q'];
+
+    $pdo = DB::getInstance();
+    if ($usernameInput !== "") {
+        if (User::GetUserBy("username", $usernameInput, $pdo)) {
+            echo "unavailable";
+        } else {
+            echo "available";
+        }
+    }
\ No newline at end of file
diff --git a/src/view/register.html b/src/view/register.html
index 4e51a2f..8438a3d 100644
--- a/src/view/register.html
+++ b/src/view/register.html
@@ -36,7 +36,7 @@
                                 Username:
                             </td>
                             <td>
-                                <input onkeyup="checkAvailability(this.value, 'username', '../model/usernameList.php')" id="username" type="text" placeholder="Username" name="username">
+                                <input id="username" type="text" placeholder="Username" name="username">
                             </td>
                         </tr>
                         <tr>
@@ -44,7 +44,7 @@
                                 Email:
                             </td>
                             <td>
-                                <input onkeyup="checkAvailability(this.value, 'email', '../model/emailList.php')" id="email" type="email" placeholder="Email address" name="email">
+                                <input id="email" type="email" placeholder="Email address" name="email">
                             </td>
                         </tr>
                         <tr>
diff --git a/src/view/script.js b/src/view/script.js
index ab16c24..ef52f8e 100644
--- a/src/view/script.js
+++ b/src/view/script.js
@@ -41,6 +41,9 @@ var isPasswordFilled = false;
 var isEmailFilled = false;
 var isPhoneFilled = false;
 var isPasswordMatch = false;
+var isUsernameAvailable = false;
+var isEmailAvailable = false;
+
 
 document.getElementById("confirm-password").onkeyup = function () {
     var confirmField = document.getElementById("confirm-password");
@@ -64,6 +67,8 @@ document.getElementById("name").onkeyup = function () {
 
 document.getElementById("username").onkeyup = function () {
     isUsernameFilled = checkRequiredField("username");
+    checkAvailability(this.value, "username", '../controller/UsernameValidationController.php');
+    isUsernameAvailable = this.classList.contains("available");
 };
 
 document.getElementById("password").onkeyup = function () {
@@ -72,6 +77,8 @@ document.getElementById("password").onkeyup = function () {
 
 document.getElementById("email").onkeyup = function () {
     isEmailFilled = checkRequiredField("email");
+    checkAvailability(this.value, "email", '../controller/EmailValidationController.php');
+    isEmailAvailable = this.classList.contains("available");
 };
 
 document.getElementById("phone").onkeyup = function () {
@@ -86,9 +93,11 @@ document.getElementById("register-form").onkeyup = function () {
         isPasswordFilled &&
         isEmailFilled &&
         isPhoneFilled &&
-        isPasswordMatch) {
+        isPasswordMatch &&
+        isUsernameAvailable &&
+        isEmailAvailable) {
         submitBtn.removeAttribute("disabled");
     } else {
         submitBtn.setAttribute("disabled", "true");
     }
-}
\ No newline at end of file
+};
\ No newline at end of file
-- 
GitLab