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 (., .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

Dani AI

Generated

Two quick notes on what’s stopping the original snippet and a small, practical approach that will work reliably.

The immediate syntax problem is declaring a variable inside the if test (for example, writing var in the condition). Also, using String.prototype.search with an unescaped dot (like .com) is actually a regular expression where . means “any character”; escape it as \. or use a suffix check instead. Finally, search() returns -1 when nothing is found, so tests should check for !== -1 (or use endsWith/slice to check the end of the string). These points explain the failure in the original post by . The method suggested by (checking against a list of extensions) is sensible—just check longer suffixes first so .co.uk is matched before .uk.

A compact, robust pattern (modern and with an old-browser fallback) is: normalize the input (trim, lowercase, strip protocol and path), iterate a suffix list ordered longest-first, detect the suffix with endsWith (or slice(-n) if endsWith is unavailable), map that suffix to a price, then populate the PayPal hidden fields right before form submission. Run the code after the DOM is ready or attach it to the form’s submit event so the values are current.

Testing and troubleshooting tips:

  • Test edge cases: user may paste a full URL (strip protocol/path), include uppercase letters, or add trailing spaces.
  • Use the PayPal sandbox to verify hidden-field behavior.
  • If compatibility with very old browsers is required, avoid endsWith and use slice as fallback.
  • Keep price/extension configuration in one place (object or JSON) so it’s easy to maintain and to add new multi-part TLDs later.

Relevant docs: String.prototype.search and String.prototype.endsWith.

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.