...

View Full Version : struggling to pull together email validation pieces



tibet000000
01-08-2011, 07:34 AM
I am trying to make a simple AJAX interaction where users type an email address into a form and when the from reaches ready state 4, the embedded java script makes a call to the server to run a PHP regexp check on the string submitted by the user, validating that it is in the right email format.

I don't really know what I am doing, and my method has been basically to grope blindly forward utilizing various internet tutorials.

However, it seems to me that what I have should work, but of course it doesn't.

so here's the html framework up to the point of interest (the double email field for validation) with the javascript snippet located in the head
:

<script language="javascript" src="checkemail.js"></script>

<form id="register" method="post" action="userdatabase.php" name="register">
<p><label>First Name: </label><input type="text" name="fname" class="text"/></p>
<p><label>Last Name: </label><input type="text" name="lname" class="text"/></p>
<p><label>Your Email: </label><input type="text" name="email" id="email" onChange="AjaxFunction();" class="text"/>
<div id="rsp_email"><!----></div>
<p><label>Re-enter Email: </label><input type="text" name="email2" class="text"/></p>


Here is the actual javascript 'checkemail.js':

<script type="text/javascript">
function AjaxFunction(email)
{
var AjaxRequest;
try
{
// Firefox, Opera 8.0+, Safari
httpxml=new XMLHttpRequest();
}
catch (e)
{
// Internet Explorer
try
{
httpxml=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
try
{
httpxml=new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e)
{
alert("Your browser does not support AJAX!");
return false;
}
}
}

ajaxRequest.onreadystatechange= function()
{
if(readystate==4)
{
document.register.email.value = ajaxRequest.ResponseText;
}
ajaxRequest.open("GET","emailcheck.php", true);
ajaxRequest.send(null);
}

}

and finally here is the php that the js utilizes- 'emailcheck.php':


if(isset(!''){
if (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)){
echo "<center>Invalid email</center>";
}else{
echo "<center>Valid Email</center>";}
}


I am sure I have lost the plot somewhere within this, as I am still struggling to conceptualize all the threads being passed around. but syntactically I thought I was doing ok, but apparently not.

Please help me figure out what the problem is somebody... anybody.

I am running this through my godaddy hosting, and MAMP to see if it works.


brian

tibet000000
01-08-2011, 07:11 PM
and here is the php file that the form posts to in the action userdatabase.php-


<?php

$fname=$_POST['fname'];
$lnam=$_POST['lname'];
$email=$_POST['email'];

$pass=$_POST['pass'];

$gender=$_POST['gender'];
$bdate=$_POST['year'].'-'.$_POST['month'].'-'.$_POST['day'];

$link=mysql_connect('xxxxxxx','brianjoseff123','xxxx');
if(!$link)
{
die('Could not connect: ' .mysql_error());
}

mysql_select_db('brianjoseff123',$link);

$query= "INSERT INTO registeredusers ('NULL' ,'fname','lname','email','pass','gender','bdate') VALUES ('$fname'.'$lname'.'$email'.'$pass'.'$gender'.'$bdate)";



?>

hdewantara
01-09-2011, 01:24 PM
Since you need only to validate email address, I would suggest to do that client side i.e. by javascript; no need to use ajax. I think I saw some good ones in forum, have you tried searching?

Hendra

devnull69
01-09-2011, 10:32 PM
Most importantly you somehow misunderstood how ajax requests work

What you did:
- The AjaxFunction() only creates the request object "httpxml". It does not start the request!
- You try to set onreadystatechange for another object "ajaxRequest" on the global level. This will be executed before AjaxFunction(), so there is no request object yet.
- You try to start a request inside the callback of ajaxRequest. The callback would be the function that has to be executed after the request's ready state changes. It can only change after it has been started though ....

Summary: Lots of basic errors

This is how ajax requests usually work
- You create the request object. Let's call it httpxml:


try {
// Firefox, Opera 8.0+, Safari
httpxml=new XMLHttpRequest();
} catch (e) {
// Internet Explorer
try {
httpxml=new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
httpxml=new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {
alert("Your browser does not support AJAX!");
}
}
}

- You call the open method of that specific object


httpxml.open('GET', url, true);

- You set the callback for that specific object


httpxml.onreadystatechange = function() {
if(httpxml.readyState == 4 && httpxml.status == 200) {
// The request has been sucessfully finished, take the output and
// do something useful with it. The output is available in
// httpxml.responseText
}
}

- You send the request


httpxml.send(null);

prasanthmj
01-10-2011, 04:48 PM
alert("Your browser does not support AJAX!");
:eek:

Ajax validation is needed when you need to some server side verification (like cross checking with a database) Else, simple Javascript + server side validation is good/enough.

nikos777
01-19-2011, 03:45 AM
thanks a lot guys!! im going to try these codes.look interesting

http://rhodesmarket.blogspot.com/



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum