View Full Version : Javascript form validation before submission

05-18-2012, 06:54 PM
Hello there, I hope you might be able to help me with an issue I'm having getting a form to validate before submission. I've been trying to figure out where I'm going wrong but being relatively new to javascript I'm obviously missing something.

I have a form which calls one of two validation scripts with onBlur for each field (I've only included one of each field, there are 8 fields in total in the form):

<form name="submitrunnertime" action="http://......reflect.php" onsubmit="return valResultsForm();" method="post">


<tr><td><label for="RunnerID">Runner ID</label></td>
<td><input type="text" name="RunnerID" size="5" maxlength="5" onBlur="valRange(RunnerID, 1, 99999, 'RunnerID_bk');" />
</td><td id="RunnerID_bk"><span></span></td></tr>

<tr><td><label for="">Date (YYYY-MM-DD)</label></td>
<td><input type="text" name="Date" size="10" maxlength="10" onBlur="valExpression(Date, '^(19[0-9{2}|20[01][0-9])\-(0[1-9]|1[012])\-([012][0-9]|3[01])$', 'Date_bk', 'please use indicated format');" />
</td><td id="Date_bk"><span></span></td></tr>

etc. etc.


<input type="submit" name="submitrunnertime" value="submit">

I've managed to get each individual field to validate as the form is filled in.

Now, if I fill valResultsForm.js with a simple line return false; then the form refuses to submit as it should do, but if I try and re-run the validation of each field so that the form will only submit if all fields have been filled in correctly, even if I force valResultsForm to return a false result, the form still submits. I just can't figure out why it is doing this and it's driving me around the bend.

valResultsForm looks like:

function valResultsForm() {

var res1 = valRange(RunnerID, 1, 99999, 'RunnerID_bk');

var res2 = valExpression(Date, '^(19[0-9{2}|20[01][0-9])\-(0[1-9]|1[012])\-([012][0-9]|3[01])$', 'Date_bk', 'please use indicated format');

if (res1 == true && res2 == true) {
return true;
else { return false; }


Any hints or advice that anyone could give me would be hugely appreciated.

Thank you, Andrew

05-21-2012, 07:39 PM
Please tell us what those two validation routines (valRange, valExpression) look like.