使用javascript验证表单中输入的URL来自特定网站 [英] Validate that a URL entered in a form is from a specific website with javascript
问题描述
我有一个网站,人们可以在其中发布来自Google+的链接.我试图确保人们只能发布来自Google plus的特定链接.例如,某人需要发布一个链接,例如 https://plus.google.com/games/907809777960/params/%22%7B%5C%22encPrms%5C%22%3A%5C%22eyJiYXBpVGlja2V0SWQiOiI4MzFhNGQ0Ny0yYTU4LTQ2OTktYmI1Yy1hN2ExYTAzY2U4ZTMiLCJsYW5kaW5nUGFnZSI6Im5ld3NmZWVkL2JvbnVzYWJsZUZlZWQvbWFydmVsY29tcGxldGUvNTQ3Mjc3LzEzMTQ0NzA0MjUvMCIsInJlZl9pZCI6IjEwOTkyODAzNzUzNzQ2Mjk5NzAxMCIsInRyYWNrIjoibmV3c2ZlZWQtYm9udXNfbWFydmVsQ29tcGxldGUtMCIsInNlbmRfdGltZXN0YW1wIjoiMTMxNDQ3MDQyNyJ9%5C%22%7D%22/source/3 .我想确保链接以开头或至少包含 https://plus.google.com/games/907809777960/params/,否则将不会提交链接并警告该链接无效.到目前为止,我的代码是.
I have a website where people post links from Google+. I am trying to make sure that people can only post specific links from Google plus. An example would be, someone would need to post a link like https://plus.google.com/games/907809777960/params/%22%7B%5C%22encPrms%5C%22%3A%5C%22eyJiYXBpVGlja2V0SWQiOiI4MzFhNGQ0Ny0yYTU4LTQ2OTktYmI1Yy1hN2ExYTAzY2U4ZTMiLCJsYW5kaW5nUGFnZSI6Im5ld3NmZWVkL2JvbnVzYWJsZUZlZWQvbWFydmVsY29tcGxldGUvNTQ3Mjc3LzEzMTQ0NzA0MjUvMCIsInJlZl9pZCI6IjEwOTkyODAzNzUzNzQ2Mjk5NzAxMCIsInRyYWNrIjoibmV3c2ZlZWQtYm9udXNfbWFydmVsQ29tcGxldGUtMCIsInNlbmRfdGltZXN0YW1wIjoiMTMxNDQ3MDQyNyJ9%5C%22%7D%22/source/3. I want to make sure that the link starts with or at least contains https://plus.google.com/games/907809777960/params/, if not, it will not submit the link and alert that the link is invalid. The code I have so far is.
<script type="text/javascript" language="JavaScript">
function checkForm(theForm) {
if (form.bonuslink.indexOf("https://plus.google.com/games/907809777960/params/") == -1)
{ alert('You can only enter authentic Google + links'); return false; }
else {
return true; }
}
</script>
<form action="submitbonus.php" onsubmit="return checkForm(this);" method="post">
Bonus Link: <input name="bonuslink" type="text" size="40" /> <input name="Submit" type="submit" value="Submit Bonus" /><br />
</form>
由于某种原因,我无法使其正常工作.无论键入什么,它都会每次提交.我对javascript不太熟悉,因此不胜感激.
I cannot get it to work for some reason. It submits every time regardless of what it typed. I am not that familiar with javascript, so any help would be appreciated.
推荐答案
编辑您有两个问题,您需要引用bonuslink值而不是DOM元素本身,并且需要将其称为"theForm"的成员而不是形式",因为这就是您所说的参数.除此之外,一切都应该没事.
edit edit: you have two problems you need to reference the bonuslink value not the DOM element itself and you need to call it as a member of the 'theForm' instead of 'form' since that is what you called the parameter. Other than that everything should be fine.
<script type="text/javascript" language="JavaScript">
function checkForm(theForm) {
if (theForm.bonuslink.value.indexOf("https://plus.google.com/games/907809777960/params/") == -1){
alert('You can only enter authentic Google + links');
return false;
} else {
return true;
}
}
</script>
<form action="submitbonus.php" onsubmit="return checkForm(this);" method="post">
Bonus Link: <input name="bonuslink" type="text" size="40" /> <input name="Submit" type="submit" value="Submit Bonus" /><br />
</form>
这篇关于使用javascript验证表单中输入的URL来自特定网站的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!