我正在尝试验证表单,但是当我按下提交按钮时,它没有做任何事情......我想让它发送到下一页是game.html。
HTML:
<!--[if IEMobile]> <link rel="stylesheet" type="text/css" href="/styles/mobile.css" media="screen"/> <![end if]--> </head> <body> <img class="notmobile" id="imgtop1" src="image/card10.jpg"> <img class="notmobile" id="imgtop2" src="image/card11.jpg"> <img class="notmobile" id="imgtop3" src="image/card12.jpg"> <img class="notmobile" id="imgtop4" src="image/card13.jpg"> <img class="notmobile" id="imgtop5" src="image/card1.jpg"> <img class="notmobile" id="imgleft1" src="image/back.jpg"> <img class="notmobile" id="imgleft2" src="image/back.jpg"> <img class="notmobile" id="imgleft3" src="image/back.jpg"> <img class="notmobile" id="imgleft4" src="image/back.jpg"> <img class="notmobile" id="imgleft5" src="image/back.jpg"> <img class="notmobile" id="imgright1" src="image/back.jpg"> <img class="notmobile" id="imgright2" src="image/back.jpg"> <img class="notmobile" id="imgright3" src="image/back.jpg"> <img class="notmobile" id="imgright4" src="image/back.jpg"> <img class="notmobile" id="imgright5" src="image/back.jpg"> <div id="title"> <h1> Grand Palace Casino </h1> </div> <div id="content"> <div id="formbox"> <fieldset> <form id="fback" name="fback" method="get" action="game.html" onsubmit="return validateForm();"> <label class="form"> First name: </label> <br /> <input type="text" maxlength="30" size="30" name="fname" id="fname" /> <br /><br /> <label class="form"> Last name: </label> <br /> <input type="text" maxlength="30" size="30" name="lname" id="lname"/> <br /><br /> <label class="form"> Phone number: </label> <br /> <input type="text" maxlength="30" size="30" name="pnum" id="pnum"/> <br /><br /> <label class="form"> Postal code: </label> <br /> <input type="text" maxlength="30" size="30" name="postCode" id="postCode"/> <br /><br /> <label class="form"> Starting amount of money: </label> <br /> <input type="text" maxlength="30" size="30" name="startMoney" id="startMoney"/> <br /><br /> <input name="button" type="button" id="button" value="Submit" /> </form> </fieldset> </div> </div> </body> </html>
的Javascript
// JavaScript Document
var $ = function(id) { return document.getElementById(id); }
function validateForm(){
var nextPage = true;
var regFName = /^([A-Z])[a-z]{1,20}$/;
var regLName = /^([A-Z])[a-z]{1,30}$/;
var regPnum = /^\d{3}\-\d{3}\-\d{4}$/;
var regPostCode = /^[A-Za-z]{1}\d{1}[A-Za-z]{1} \ *\d{1}[A-Za-z]{1}\d{1}$/;
var myMoney = $("startMoney").value;
var regMoney = /[\.\,]/;
document.fback.postCode.value = document.fback.postCode.value.toUpperCase();
if ($("fname").value == "")
{
alert ("Enter your first name");
nextPage = false;
}
else if (!$("fname").value.match(regFName))
{
alert ("You must capitalize your first character, and then the rest are lower-case. Only letters allowed. E.G: Charlie");
nextPage = false;
}
else if ($("lname").value == "")
{
alert ("Enter your last name");
nextPage = false;
}
else if (!$("lname").value.match(regLName))
{
alert ("You must capitalize your first character, and then the rest are lower-case. Only letters allowed. E.G: Kaing");
nextPage = false;
}
else if ($("pnum").value == "")
{
alert ("Enter your phone number");
nextPage = false;
}
else if (!$("pnum").value.match(regPnum))
{
alert ("You must use numbers only. Enter your phone in the following format: ###-###-#### E.G: 613-600-6991");
nextPage = false;
}
else if ($("postCode").value == "")
{
alert ("Enter your postal code");
nextPage = false;
}
else if (!$("postCode").value.match(regPostCode))
{
alert ("Enter your postal code in the following format: ANA NAN");
nextPage = false;
}
else if ($("startMoney").value == "")
{
alert ("Enter your starting amount of money");
nextPage = false;
}
else if (isNaN(myMoney) || parseInt(myMoney) < 5 || parseInt(myMoney) > 5000 || $("startMoney").value.match(regMoney))
{
alert ("You must enter money between 5$ and $5000. Only numbers allowed. No cents. E.G: 5000");
nextPage = false;
}
nextPage = true;
}
答案 0 :(得分:2)
你的函数没有返回任何内容,并且@fizz声明你的输入类型应该是type =&#34;提交&#34;而不是&#34;按钮&#34;。
...
else if (isNaN(myMoney) || parseInt(myMoney) < 5 || parseInt(myMoney) > 5000 || $("startMoney").value.match(regMoney))
{
alert ("You must enter money between 5$ and $5000. Only numbers allowed. No cents. E.G: 5000");
nextPage = false;
}
return nextPage;
}
答案 1 :(得分:0)
在jquery中访问元素语法的id是$(“#elementID”),并且通过使用val()fn来获取元素的值。
离。
if ($("#fname").val() == "")
{
alert ("Enter your first name");
nextPage = false;
}
var myMoney = $("#startMoney").value;