My validation is in my php script encoded in json, I'm not sure how to implement it in my main JavaScript function.
I'm using regular expressions to in my php script to validate the form criteria, I need to pass this to the JavaScript file and return a success massage for for each form id.
this is what I have so far.
$(document).ready(function()
{
$("#submit").click(function()
{
// Clear any success or error messages
$("#success").html("");
$("#errors").empty();
//make an AJAX call here, and set error or success accordingly
$.post('backend.php',{act:'validate'},
function(getData)
{
//unsure about this function
}
});
// prevents submit button default behavior
return false;
});
});
<html>
<head>
</head>
<body>
<h1>Form Validation</h1>
<form id="PersonForm">
Name:
<input id="name" type ="text" name="name"></input>
<br><br>
Postal Code:
<input id="postal" type ="text" name="postal"></input>
<br><br>
Phone Number:
<input id="phone" type ="text" name="phone"></input>
<br><br>
Address:
<input id="address" type ="text" name="address"></input>
<br><br>
<input type="submit"></input>
</form>
<div id= "content"></div>
<a href="frontend.html">Refresh</a>
<a id="InsertDefault" href="#">Insert Default Data</a>
<br><br>
<p id='msg'></p>
<ul id="errors"></ul>
<p id="success"></p>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<script src="main.js" type="text/javascript"></script>
</body>
</html>
<?php
if ($_REQUEST['act'] == 'validate')
{
$validateData = array();
if (preg_match("/^[A-Za-z]{3,20}$/",$_REQUEST['name'])) $validateData['name'] = 1;
else $validateData['name'] = 0;
if (preg_match("/^[0-9]{10}$/",$_REQUEST['phone'])) $validateData['phone'] = 1;
else $validateData['phone'] = 0;
if (preg_match("/^[A-Z][0-9][A-Z][0-9][A-Z][0-9]$/", $_REQUEST['postal'])) $validateData['postal'] = 1;
else $validateData['postal'] = 0;
if (preg_match("/^[0-9]{3} [A-Za-z]{3,10} Street$/", $_REQUEST['address'])) $validateData['address'] = 1;
else $validateData['address'] = 0;
echo json_encode($validateData);
}
else echo "Should not happen";
?>
</div>