How can i use javascript to split a domain name into two variables on a form before posting?
I have a an input box on my homepage which i want users to input their domain to search for . E.G.
test.com
or
test.gb.com
etc and i would like it to on submission ( post ) to extract the everything upto the first "." into a variable $domain and everything after the first including "." into $ext
So $domain might now contain "test" and $ext would contain ".gb.com" When the form was posted.
Possible ?
Here is the code for my form so far . What i need is to have everything that is inputted upto the first "." stored in the name="domain" form variable and ever开发者_如何学运维ything in this line i.e from the $ext variable stored for the form submission
Code so far :
<form method="post" action="https://www.jms-networks.net/domainchecker.php">
<span class="text"><input value="{php} print $_POST['sld'] ? $_POST['sld'] : 'Search for your domain name'; {/php}" type="text" name="domain" class="searchbox" onfocus="if (this.value == this.defaultValue) this.value = '';" onblur="if (this.value == '') this.value = this.defaultValue;" /></span>
<input type="hidden" name="direct" value="true" />
<!-- This one below should contain the $ext value everythign after including the first "." -->
<input type="hidden" name="tlds[]" value="what goes here???" />
<input type="hidden" name="tlds[]" value=".me.uk" />
<input type="hidden" name="tlds[]" value=".org.uk" />
<input type="hidden" name="tlds[]" value=".com" />
<input type="hidden" name="tlds[]" value=".net" />
<input type="hidden" name="tlds[]" value=".org" />
<input type="submit" value="Go" class="product-headers" />
</form>
var i = $thewhole.indexOf('.');
var $domain = $thewhole.substr(0, i);
var $ext = $thewhole.substr(i);
Or with regexp
var regexp = /([^.]+)(\..*)/;
var match = regexp.exec($thewhole);
var $domain = match[1];
var $ext = match[2];
Edit:
<script type="text/javascript">
function submithelper() {
// this is why we need the id="xxx" below.
var $domainInput = document.getElementById("domain");
var $extInput = document.getElementById("ext");
var $domain = $domainInput.value;
var $i = $domain.indexOf('.');
var $host = $domain.substr(0, $i); // perhaps skip to do this if it isn't used?
var $ext = $domain.substr($i);
$extInput.value = $ext;
return true; // false to stop form from submitting.
}
</script>
<form onsubmit="return submithelper()" method="post" action="https://www.jms-networks.net/domainchecker.php">
<!-- Two inputs has been given IDs so we can easily get them from javascript. -->
<span class="text"><input id="domain" value="{php} print $_POST['sld'] ? $_POST['sld'] : 'Search for your domain name'; {/php}" type="text" name="domain" class="searchbox" onfocus="if (this.value == this.defaultValue) this.value = '';" onblur="if (this.value == '') this.value = this.defaultValue;" /></span>
<input type="hidden" name="direct" value="true" />
<input id="ext" type="hidden" name="tlds[]" value="" />
...
精彩评论