根据第一个字段使用 Javascript 填充表单字段
我有一个小问题,我正在尝试寻找最佳解决方案。我有一个表单,用户将在其中输入他们复制的网址,它的格式始终类似。
像这样:http://www3.google.com/register.php?key=12334123< /a>
我希望能够根据上述输入填充两个字段。
字段 1 将是服务器(子域)
字段 2 将是键号。
我目前已经制作了一些有效的东西,但是我认为使用正则表达式来完成它比我正在做的方式更好。
对java不太热衷,这对我来说是一个挑战,任何帮助将不胜感激!
<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>
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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
与 url 的主机部分和关键参数相匹配的简单正则表达式是:
您正在询问服务器/子域,因此如果您想要“www3”
A simple regex that matches the host part and key parameter of the url would be:
You are asking for the server/subdomain, so if you are wanting the 'www3'