For whatever reason my validation isn't showing, will post in three parts, seems the forum doesn't like part of the code
Just to clarify I mean it's not showing in the error spans, it shows in the console log
https://gotsocial.co.uk/fife/register.php <--live version
<?php
require_once 'connection.php';
$errors = [];
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
$username = trim($_POST['username']);
$email = trim($_POST['email']);
$password = trim($_POST['password']);
if (filter_var($_POST['username'], FILTER_VALIDATE_REGEXP, ["options" => ["regexp" => "/.{3,25}/"]]) === FALSE)
{
$errors[] = ["name" => "username", "error" => "Invalid username (3 to 25 characters)"];
}
if(filter_var($_POST['username'] , FILTER_VALIDATE_REGEXP,["options"=> [ "regexp" => "/^[\p{L}0-9\s]+$/u"]]) === FALSE)
{
$errors[] = ["name" => "username", "error" => "Usernames may not contain symbols"];
}
if (filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) === FALSE)
{
$errors[] = ["name" => "email", "error" => "invalid Email"];
}
if (filter_var($_POST['password'], FILTER_VALIDATE_REGEXP, ["options" => ["regexp" => "/.{6,25}/"]]) === FALSE)
{
$errors[] = ["name" => "password", "error" => "Invalid password (6 to 25 characters)"];
}
if (!preg_match("/(?=[a-z]*[0-9])(?=[0-9]*[a-z])([a-z0-9-]+)/i", $_POST['password']))
{
$errors[] = ["name" => "password", "error" => "Password must contain numbers and letters"];
}
if ($_POST['password'] !== $_POST['repeatPassword'])
{
$errors[] = ["name" => "repeatPassword", "error" => "passwords don't match"];
}
if (count($errors) === 0)
{
// everything is OK, the browser should send us to the next page
try
{
$createNewUser = $db->prepare('INSERT INTO usertable (username, email, password) VALUES (:username, :email, :password)');
$createNewUser->bindValue(':username', $_POST['username']);
$createNewUser->bindValue(':password', password_hash($_POST['password'], PASSWORD_DEFAULT));
$createNewUser->bindValue(':email', $_POST['email']);
$createNewUser->execute();
$success = ["message" => "Please check your registered E-mail address to activate your account"];
}
catch(Exception $e)
{
$errors[] = ["name" => "username", "error" => "There was a problem contact admin"];
}
}
}
header('Content-Type: application/json');
if (isset($success))
{
echo json_encode($success);
}
else
{
echo json_encode(["errors" => $errors]);
}