-1

I'm trying to validate the form, but when I press the submit button, it doesn't do anything... I want it to send me to the next page which is 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;
}
1
  • Check javascript console log. Also for a quick debug, working with all navigator, add alerts inside you chekform function to detect where it stops. Commented Mar 21, 2014 at 21:50

2 Answers 2

2

Your function does not return anything, and as @fizz stated your input type should be type="submit" and not "button".

...
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;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Didn't warrant its own answer but your submit button should be of type submit not button. Else it won't call the onsubmit handler.
0

In jquery to access id of an element syntax is $("#elementID") and to get the value of an element is by using val() fn.

ex.

   if ($("#fname").val() == "")
{
    alert ("Enter your first name");
    nextPage = false;
}

var myMoney = $("#startMoney").value;

1 Comment

Include Jquery library to accress jquery object.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.