How to validate data in an AJAX call
<p>I am trying to call data from a PHP file where it takes the data entered and tells if it is validated or not. How do you do this in the javascript file using an AJAX call? </p>

<pre><code>$("#PersonForm").submit(function()
{
    $.ajax({
        url: 'backend.php', type: 'post', data: { act:'validate'},
        dataType: 'json',
       function(result) {
            if($validateData==1){
                $('#success').html('validated');
            }
            else{
                $('#errors').html('Not Correct');
            }
        }

        //});

    });
    return false;

 });
</code></pre>

<p>Here is the PHP file</p>

<pre><code>&lt;?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";

?&gt;
</code></pre>

<p>HTML file: </p>

<pre><code>&lt;html&gt;
&lt;body&gt;

&lt;h1&gt;Form Validation&lt;/h1&gt;

&lt;form id="PersonForm"&gt;

Name: &lt;input type="text" id="name" name="name"&gt; &lt;br&gt;

Postal Code: &lt;input type="text" id="postal" name="postal"&gt; &lt;br&gt;

Phone Number: &lt;input type="text" id="phone" name="phone"&gt; &lt;br&gt;

Address: &lt;input type="text" id="address" name="address"&gt; &lt;br&gt;

&lt;input id="sub" type="submit"&gt;

&lt;/form&gt;

&lt;a href="frontend.html"&gt;Refresh&lt;/a&gt; 
&lt;a id="InsertDefault" href=""&gt;Insert Default Data&lt;/a&gt; 
&lt;br&gt;

&lt;ul id="errors"&gt;&lt;/ul&gt;
&lt;p id="success"&gt;&lt;/p&gt;

&lt;/body&gt;
&lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"&gt;&lt;/script&gt;
&lt;script type="text/javascript" src="main.js"&gt;&lt;/script&gt;
&lt;/html&gt;
</code></pre>