[SOLVED]having trouble validating with ajax/php...
Posted: Fri May 04, 2007 7:54 am
feyd | Please use
Which is picked up by the javascript...
Now, that all works well, i can send and receive data with my php file..
But i think the problem is here somewhere...
feyd | Please use
Code: Select all
,Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
I hope you can help with this because I really cant fugure it out! i think i might be the Validate class that holds the problem but i'll post all the code incase im wrong...
It's not validating the given string as I expect it to, so the $validator->isEmail() function always returns false.
My text input box sends this request...Code: Select all
onblur="sendReq(this.value, this.id, 'email_valid');"Code: Select all
function createXmlHttp() {
var xmlHttp;
try {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {
try {
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
xmlHttp = new XMLHttpRequest();
}
}
return xmlHttp;
}
function sendReq(str, objId, responseId) {
var obj=document.getElementById(objId);
var response=document.getElementById(responseId);
if (str.length==0) {
response.innerHTML='';
return;
}
var xmlHttp=createXmlHttp();
if (xmlHttp==null) {
alert ("Your browser does not support AJAX!");
return;
}
var url="process.php?"+obj+"="+escape(str)+"&a="+Math.random();
xmlHttp.onreadystatechange=function() {
if (xmlHttp.readyState==4 && xmlHttp.status==200) {
response.innerHTML=xmlHttp.responseText;
}
}
xmlHttp.open("GET",url);
xmlHttp.send(null);
}
Code: Select all
require_once("lib/Validator.php");
$str = urldecode($_GET['email']);
$validator = new Validator($str);
if ($validator->isEmail()) {
echo "<img src='img/tick.gif' />";
} else {
echo "<img src='img/cross.gif' />";
}Code: Select all
class Validator {
var $str;
var $pattern;
function Validator($str) {
$this->str=$str;
}
function isEmail() {
if (!preg_match("/^[a-z0-9._-]+@[a-z0-9.-]+\.[a-z]{2,4}$/i", $this->str)) {
return false;
} else {
return true;
}
}
}feyd | Please use
Code: Select all
,Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]