Following on with subdomains, say you want one field to auto-suggest into another. For example, when a user enters their company name (or username, last name etc.) this same value could be used to suggest a valid subdomain. Here’s the javascript and html (the function uses prototype for the element lookup).
The suggestion is based on the facts that valid subdomains should be alphanumeric, with dashes, less than 63 chars and not start or end with a dash.
<label for="username">Username</label>
<input id="username" onkeyup="suggestSubdomain(value, 'subdomain');" type="text" name="username" /><br/>
<label for="subdomain">Subdomain</label>
<input id="subdomain" type="text" name="subdomain" />
<script type="text/javascript">
function suggestSubdomain(value, element) {
var subdomain = value.gsub(/\s+/, '-');
subdomain = subdomain.gsub(/[^a-z0-9\-]/i, '');
if(subdomain[0] == '-')
subdomain = subdomain.substr(1, subdomain.length)
if(subdomain[subdomain.length-1] == '-')
subdomain = subdomain.substr(0, subdomain.length-1)
$(element).value = subdomain.substr(0, 63).toLowerCase();
}
</script>