Hey,

Simple javascript problem:

user enters desired domain name into "domain" field and submits, php scripts check if domain is available. If it is, a paypal add to basket button is written onto the page, however i cannot use php to get the content of the "domain" field and put it into hidden fields of the paypal button for various reasons so instead I need to use javascript. Also i want to search what the user entered to find out what kind of domain suffix they typed (.co.uk, .com etc) and write a variable with a corresponding price. I have VERY little experience with javascript and am only really able to code what is below from knowledge of php and tutorials but the script isnt working. any ideas?

var domain = document.getElementById("domain").value;

var domainsuffix1 = (domain.search(/.com/i));
if (var domainsuffix1 > 0) {var domainsuffix = "£5";};

Thanks,


Max

Someone will probably blow me away with some regular expression but this is what I would do for the domain extension:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<script language="javascript" type="text/javascript">
function getExtension()
{
	var domainname = document.getElementById('txtDomain').value;
	var extensions = new Array(".com", ".co.uk", ".net", ".org"); //add other desired extensions
	
	for(i = 0; i < extensions.length; i++)
	{
		if(domainname.substr(domainname.length - extensions[i].length, extensions[i].length) == extensions[i])
		{
			document.getElementById('txtExtension').value = extensions[i];
		}
	}
}
</script>
</head>

<body onLoad="getExtension('daniweb.com');">
<input type="text" id="txtDomain" name="txtDomain" onBlur="getExtension('txtDomain');" />
<input type="text" id="txtExtension" name="txtExtension" />
</body>
</html>

And then just use the same document.getElementById('txtDomain').value = 'desired value'; idea for populating the domain text field

This article has been dead for over six months. Start a new discussion instead.