I have a slight issue I’m trying to find the best solution for. I have a form where users will enter a url that they copy, it will always be formatted similarly.
Like this: http://www3.google.com/register.php?key=12334123
I would like to be able to populate two fields based on the above input.
Field 1 will be the server (subdomain)
Field 2 will be the key number.
I currently have made something that works, however I think it would be better to do it with regex as opposed to the way I’m doing it.
Being not too keen on java this is a bit of a challenge for me, any help would be greatly appreciated!
<script language="javascript">
function urlbreaker(url)
{
var startserver = document.form1.url.value.search(/www/)
document.form1.server.value = document.form1.url.value.substr(startserver, 4)
var startkey = document.form1.url.value.search(/=/)
document.form1.key.value = document.form1.url.value.substr(startkey+1, 10)
}
</script>
<form id="form1" name="form1" method="post" action="">
<p>
<label for="url">input url:</label>
<input type="text" name="url" id="url" onblur="urlbreaker()" />
http://www3.google.com/register.php?key=12334123</p>
<p>
<label for="server">Server:</label>
<input type="text" name="server" id="server" />
should be www3</p>
<p>
<label for="key">key</label>
<input type="text" name="key" id="key" />
should be 12334123</p>
</form>
1 Answer