dcsimg
www.webdeveloper.com
Results 1 to 6 of 6

Thread: final field validated determines form submission

  1. #1
    Join Date
    Jan 2003
    Posts
    3

    final field validated determines form submission

    When I validate several fields "on submit" the form is submitted if the final field checked is valid even if other fields checked earlier in the order are not valid. The non-valid fields are reported properly but the form is submitted anyway as long as the final field is valid.

    I think I understand why this happens, given the logic of the scripts involved, but I still don't know how to ensure that all fields are valid before submission. As I understand it, validating field by field, for example "on blur", only works if the user actually enters and then exits a field. If they don't enter the field the validation is not performed.
    What is the most efficient way to ensure that all fields, not just the final field checked, are valid prior to submission? One solution is forcing a set progression from field to field. Can this be done and over-ride a simple mouse click to a field not in sequence?

    Obviously, I am fairly new to html. However, I've spent some time on this without resolution and could really use some help.

    TIA.

    Dave

  2. #2
    Join Date
    Nov 2002
    Posts
    631
    Could you post the script your using?

    You could do something like:

    <script type="text/JavaScript">

    for (a = 1; a < 3; a++) { // 3: number of fields;
    if (document.formName.fieldName[a].length < 6) {
    alert("The field must contain at least 6 characters");
    }
    if (document.formName.fieldName[a].value == "") {
    alert("You must enter a value.");
    }
    }

    </script>

    This might have the same problem as your script, but I don't know.

    This script has not been tested.
    Last edited by Zach Elfers; 01-19-2003 at 12:10 PM.

  3. #3
    Join Date
    Jan 2003
    Posts
    3
    Sorry for the long post below. I am including more, rather than less because of my inexperience. I think the first script is not related to my current problem. The relevant script begins at "function MM_validateForm() { //v4.0". As I understand it the form action then calls this script for each of the fields designated and with the parameters given.

    My problem is that entering a number between 60 and 400 for "Fee Total" makes this field valid and the script is submitted even though "First Name Guardian 1" is blank.

    TIA.

    Dave

    <script language="JavaScript" type="text/JavaScript">
    <!--
    function MM_findObj(n, d) { //v4.01
    var p,i,x; if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
    if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
    for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
    if(!x && d.getElementById) x=d.getElementById(n); return x;
    }

    function MM_validateForm() { //v4.0
    var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
    for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=MM_findObj(args[i]);
    if (val) { nm=val.name; if ((val=val.value)!="") {
    if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
    if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.\n';
    } else if (test!='R') { num = parseFloat(val);
    if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';
    if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
    min=test.substring(8,p); max=test.substring(p+1);
    if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
    } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; }
    } if (errors) alert('The following error(s) occurred:\n'+errors);
    document.MM_returnValue = (errors == '');
    }
    //-->
    </script>

    ...

    <FORM ACTION="http://www.formhandler.com/service?144013" METHOD=POST onSubmit="MM_validateForm('First Name Guardian 1','','R');MM_validateForm('Fee total','','RinRange60:400');return document.MM_returnValue">

    ...

    TIA Dave

  4. #4
    Join Date
    Nov 2002
    Posts
    631
    The reason that your script is not working is because the onSubmit handler only validates what the parameters given are. To keep everthing validated, do something like this:

    <input onBlur="MM_validateForm('First Name Guardian 1','','R');">

    Do that for every field and enter new parameters and it should work.

    Or you might be able to just reverse it like this:

    <FORM ACTION="http://www.formhandler.com/service?144013" METHOD=POST onSubmit="MM_validateForm('Fee total','','RinRange60:400');MM_validateForm('First Name Guardian 1','','R');return document.MM_returnValue">

  5. #5
    Join Date
    Jan 2003
    Posts
    3
    Thanks for the reply.

    I tried reversing the validation order as you suggest but the form is still submitted when the second but not the first field is valid.

    I think that

    <input onBlur="MM_validateForm('First Name Guardian 1','','R');">

    will also only work if a user enters each of the designated fields.

    How does one keep track of whether any field has failed vaildation at the time of submissions and return then the user to the form page, without submission, to correct those fields?

    Dave

  6. #6
    Join Date
    Jan 2003
    Location
    Dundee, Scotland
    Posts
    1,367
    Hi,
    i would do this using some javascript and some ASP.

    <script language="JavaScript">
    <!--

    function MM_validateForm() { //v4.0
    var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
    for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=MM_findObj(args[i]);
    if (val) { nm=val.name; if ((val=val.value)!="") {
    if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
    if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.\n';
    } else if (test!='R') {
    if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';
    if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
    min=test.substring(8,p); max=test.substring(p+1);
    if (val<min || max<val) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
    } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; }
    } if (errors) alert('The following error(s) occurred:\n'+errors);
    document.MM_returnValue = (errors == '');
    }
    </script>

    then have your form tag like this:

    <form name="form1" method="post" action="/services/E-CommerceQuote_new.asp" onSubmit="MM_validateForm('FirstName','','R','Surname','','R','CompName','','R','Cadd1','','R','City ','','R','Region','','R','Country','','R','Postcode','','R','Phone','','RisNum','Fax','','NisNum','E mail','','RisEmail');return document.MM_returnValue">


    then have your form elements on your page:

    this will only validate textboxes and text areas and will not validate radio buttons and checkboxes.

    so now if you post the form back to the same page you can use some ASP to help vaidate the rest then ddirect the user to the required page.


    for example:

    start your page like this
    <%

    if submit = "yes" then

    '----------start to validate the form--------------

    else

    '---------display the form-----------------------

    end if

    %>

    ok so what im saying is that if the submit button has been click then the form is posted back to its self it runs this code gets a yes from the submit button so it runs the rest of your validate, but if the sumbit value is "nothing" then that means its the first time in and the form has not be submitted yet therefore display the form.


    ok so now you need to put some validation code in

    <%

    if submit = "yes" then

    '----------start to validate the form--------------

    if textbox1 = "" then
    textbox1error = "empty"
    else
    textbox1error = ""
    end if
    if radiobutton1 = "" then
    radiobutton1error = "empty"
    else
    radiobutton1 = ""
    end if

    '---------ok so you need to send to the correct place------
    if textbox1error = "" and radiobutton1error = "" then
    response.redirect "/newpage.asp"
    else
    '-----------------Display your form again and show any errors
    end if


    else

    '---------display the form-----------------------

    end if

    %>
    hope this helps

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles