Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    May 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Javascript form validation before submission

    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):

    Code:
        <form name="submitrunnertime" action="http://......reflect.php" onsubmit="return valResultsForm();" method="post">
    
        <table>
          
          <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.
    
        </table>
        
        <input type="submit" name="submitrunnertime" value="submit">
        </form>
    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:

    Code:
    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

  • #2
    Regular Coder
    Join Date
    Jun 2002
    Location
    Flint, Michigan, USA
    Posts
    617
    Thanks
    1
    Thanked 20 Times in 20 Posts
    Please tell us what those two validation routines (valRange, valExpression) look like.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •