Sunday, May 31, 2009

[jQuery] split jquery.format value with the validate plugin

Is it possible to split a jQeury.format value?

javascript
$().ready(function() {


$("#check").validate({

rules: {
email: {
required: true,
email: true,
remote: "domain_check.php"

},


},

messages: {
email: {
email: "Please type a valid email address",
required: "Your email address.",
remote: jQuery.format("The domain entered does not {0} exists. Or
you made a type error.")

}
});

});

php script: domain_check.php. script is taken from http://www.devshed.com
<?php
$request = trim(strtolower($_GET['mail']));


list($userName, $hostName) = split("@", $request);
// Function to check whether a given hostName is a valid email
// domain address.
function myCheckDNSRR($hostName, $recType = '')
{


if(!empty($hostName)) {
if( $recType == '' ) $recType = "MX";
exec("nslookup -type=$recType $hostName", $result);
// check each line to find the one that starts with the host
// name. If it exists then the function succeeded.
foreach ($result as $line) {
if(eregi("^$hostName",$line)) {
return true;
}

}

// otherwise there was no mail handler for the domain
return false;
}
return false;
}
$valid = 'false';

// If you are running this test on a Windows machine, you'll need to
// uncomment the next line and comment out the checkdnsrr call:
if (myCheckDNSRR($hostName,"MX"))
//if (checkdnsrr($hostName,"MX"))
$valid = 'true';

else
$valid = 'false';
echo $valid;
?>

The php script checkes the part after the '@' to see it the entered
domain exists. If not an error message is revealed with remote:
jQuery.format("The domain entered does not {0} exists. Or you made a
type error.")

Now {0} will show the whole email address. But I want only the second
part after the '@' to be displayed

I've tried to do this with php but it won't split the email address

remote: jQuery.format("{0}" + "<?php $mail = "{0}"; list($userName,
$hostName) = split('@', $mail); echo $hostName;?>")

$mail = "{0}" works. echo $mail also works

but echo $hostName will show an empty string. echo $userName will show
the whole email address.

Can this be done with php or should i use javascript to split the
email address and how?

No comments: