The problem is you're using regular expressions completely wrong. For one, you don't even need them in this instance. Since you will always have a 5 digit number, and I'm assuming that your 5 digit number must reside inside the $areas array, then you'll have a finite list of valid values, not an infinite number of variable answers.
So you can easily use [man]in_array/man to see if the 5-digit value is in there. Something like:
$areas = array('12345', '54321', '00000', '11111'); // add more zip-codes as you please
if(strlen($_GET['area']) == 5)
{
if(in_array($_GET['area'], $areas))
{
$return = true;
}
else
$return = 'Area code not within valid list of area codes.';
}
else
$return = 'Area code must be 5 digits long.';
At that point you now have a couple issues. One is that the area code won't be available inside the posted form. You have the area code in a separate form which means that it won't be submitted when your main form is. So you need to put the area code field inside the other form.
Secondly you only output the result string to the browser. If you were using this as a type of form-validation prior to submission (which you should) then the form would never be submitted. You should move the CheckArea function call from the area code input onclick event to the forms onsubmit event. Even still, if you don't want to do validation with javascript before you post the form, you should use onblur inside the area code element, not onclick.
And lastly, you should ALWAYS recheck inside your receiving script that is posted to for correct values. No matter what, you can't always trust that you got the same information that you have previously verified. It is entirely possible that data may have been changed, or tampered with during transport to your server, or even the validation skipped. So be careful.
One final note. I'd suggest you use JSON to send the data back via the ajax query. I suggest this for two reasons. One is that you could send back the boolean "true" when there is a valid area code (like I have done in my code above) or a string noting the error. In javascript you can then check the type of element that was returned (a string or a boolean). If it's boolean, you can check whether it's true or false. You can then display the string (or general error message) and stop the form from processing further. An example of sending JSON back might be:
// ... My code above ends here...
if(is_string($result))
{
$result = false;
$message = $result;
}
else
$message = '';
$json = '[{"result":' . $result . ', "message":"' . $message . '"}]';
return $json;
Then in javascript something like this:
<script language="javascript" type="text/javascript">
/* ... your other functions up here ... */
function CheckArea(area){ // This function we will use to check to see if a username is taken or not.
xmlHttp=GetXmlHttpObject() // Creates a new Xmlhttp object.
if (xmlHttp==null){ // If it cannot create a new Xmlhttp object.
alert ("Browser does not support HTTP Request") // Alert Them!
return // Returns.
} // End If.
var url="check.php?area="+area // Url that we will use to check the area.
xmlHttp.open("GET",url,true) // Opens the URL using GET
xmlHttp.onreadystatechange = function () { // This is the most important piece of the puzzle, if onreadystatechange is equal to 4 than that means the request is done.
if (xmlHttp.readyState == 4) { // If the onreadystatechange is equal to 4 lets show the response text.
var resp = eval(xmlHttp.responseText); // Parse the JSON response
if(resp[0]['result'] == true)
return true;
document.getElementById("arearesult").innerHTML = resp[0]['message']; // Updates the div with the response text from check.php
return false; // Stop processing the form
} // End If.
}; // Close Function
xmlHttp.send(null); // Sends NULL instead of sending data.
} // Close Function.
</script>
Oh, I also believe that Javascript is supposed to have a semi-colon as the line terminator (much like php). So all of your lines need to have ";" finishing them off. Most of yours don't.
Hope that helps.