Jump to content

Check Email Availability (make sure email is unique in the database)


Recommended Posts

Hello everyone,

 

I'm working on a custom Joomla user registration form, and I need to make sure that both the username and email address are unique.  I have the username working great, but I'm having a problem with the email address, the script keeps removing the AT @ symbol from the search so it returns that the address is available when it's not.  This is driving me crazy...  I'm not sure if it's the JavaScript or the PHP.

 

On to the code:

window.addEvent("domready",function(){
var box = $('emailStatus');
var cun = document.id('email1');
$(cun).addEvent("blur",function(){
	if ( $(cun).value.length > 0 ){
		var url="index.php?option=com_conferencesystem&format=raw&view=checkavailability&email="+$(cun).value;
		box.style.display="block";
		box.set('html','Checking email availability...');
		var a=new Request.JSON({
			url:url,
			onComplete: function(response){
				box.set('html',response.html + '</span>');
			}
		});
		a.get();
	}
});
});

 

function chkEmail(){
if($email = JRequest::getVar('email','','get','cmd')){
	$db =& JFactory::getDBO();
	$query = 'SELECT id FROM #__users WHERE email = ' . $db->Quote($email);
	$db->setQuery($query);
	$result=$db->loadObject();
	if ($result){
		$response['html'] = '<span class="unavailable">' . $email . ' Is Not Available!</span>';
		$response['msg'] = 'false';
	}else{
		$response['html'] = '<span class="available">' . $email . ' Is Available!</span>';
		$response['msg'] = 'true';
	}
}else{
	$response['html'] = '<span class="invalid">' . $email . ' Is An Invalid Email...</span>';
	$response['msg'] = 'false';
}
echo json_encode($response);
return true;
}
if(JRequest::getVar('email','','get','cmd') != ''){
chkEmail();
}

 

encodeURIComponent() maybe the thing that is missing there... This function encodes special characters and encodes the following characters: , / ? : @ & = + $ #

 

try

var emailToCheck = encodeURIComponent( $(cun).value );

 

You don't need to decode it on your server either, that's done automatically...

 

Hope thats all it is :)

 

Hello John,

 

Thanks for the reply, I ended up getting it to work late last night, I found that was the problem, and that the PHP on the Joomla side was also removing any symbols from the var, I found if I removed CMD from the getVar it started working.  So I had JavaScript encode the URL and then PHP decode it.  Below is the working code, maybe it will help others down the road.

 

window.addEvent("domready",function(){
var box = $('emailStatus');
var cun = document.id('email1');
$(cun).addEvent("blur",function(){
	if ( $(cun).value.length > 0 ){
		var email = encodeURIComponent( $(cun).value );
		var url = "index.php?option=com_conferencesystem&format=raw&view=checkavailability&email=" + email;
		box.style.display="block";
		box.set('html','Checking email availability...');
		var a=new Request.JSON({
			url:url,
			onComplete: function(response){
				if(response.msg == "true"){
					box.set('html', '<span class="available">' + response.html + '</span>');
					cun.setAttribute("class", "inputbox textfieldValidState");
				}else{
					box.set('html', '<span class="unavailable">' + response.html + '</span>');
					cun.setAttribute("class", "inputbox textfieldRequiredState");
				}
			}
		});
		a.get();
	}
});
});

function chkEmail($email){
$db =& JFactory::getDBO();
$query = "SELECT id FROM #__users WHERE email = '" . $email . "'";
$db->setQuery($query);
$result=$db->loadObject();
if ($result){
	$response['html'] = $email . ' Is Not Available!';
	$response['msg'] = 'false';
}else{
	$response['html'] = $email . ' Is Available!';
	$response['msg'] = 'true';
}
echo json_encode($response);
return true;
}
if($email = rawurldecode(JRequest::getVar('email','','get'))){
chkEmail($email);
}

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.