Executing a function only after success validation

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • cryme
    New Member
    • Aug 2006
    • 2

    Executing a function only after success validation

    Im having a minor, simple problem. Basicly i have two different scripts they both work separately and together but just that both executes at the same time.

    On my html page i have a form, one of the functions are that you can fill in the fields and when you press on a button you go to a new page, were all filled fields are shown.

    Now i want to validate the form before i pass on the filled information to the "next page".

    But right now when you click on the button, you get your form validated as normal and then you just get sent to the "next page" even if you fail to meet up the req.

    <input name="submit" value="submit" onClick="compil e(); validate(this.f orm); return document.formSu bmit;" type="button">
    JS code:

    function validate(form){
    var error = "";
    //for each form element
    for(var i=0; i<form.length; i++){
    var element = form[i];
    //if required
    if(element.getA ttribute("requi red") == "yes"){
    //if form element if empty
    if(!valid(eleme nt.value,elemen t.getAttribute( "validate"),ele ment))
    error += element.getAttr ibute("message" ) + "\r\n";
    }
    }
    if(error != ""){
    alert(error);
    document.formSu bmit = false;
    }
    else
    document.formSu bmit = true;
    }

    function valid(value,typ e,element){
    if(value == "")
    return false;

    switch(type){
    case "int":
    if(isNaN(parseI nt(value)))
    return false;
    break;
    case "float":
    if(isNaN(parseF loat(value)))
    return false;
    break;
    case "email":
    var p = value.indexOf(' @');
    if(p<1 || p==(value.lengt h-1))
    return false;
    break;
    case "checked":
    if(!element.che cked)
    return false;
    break;
    default://string
    break;
    }
    return true;
    }

    //gather information
    //pass on to "kontakta_print .html".
    function compile() {
    var falt = escape(document .form1.fornamn. value) + "&" +
    escape(document .form1.efternam n.value) + "&" +
    escape(document .form1.gatuadre ss.value) + "&" +
    escape(document .form1.postnumm er.value) + "&" +
    escape(document .form1.postort. value) + "&" +
    escape(document .form1.tfnnumme r.value) + "&" +
    escape(document .form1.epost.va lue) + "&" +
    escape(document .form1.meddelan de.value);
    location.href = "kontakta_print .html?" + falt;
    }

    //get information from previous form
    function getInfo() {
    var find = location.search ;
    var get = find.substring( 1);
    var lista = get.split("&");
    lista[0] = unescape(lista[0]);
    return lista;
    }

    //funtion for printout
    function skrivUt() {
    var skriv = getInfo();
    document.write( "<p><b>F&ouml;r namn: </b>" + skriv[0] + "<p>");
    document.write( "<p><b>Efternam n: </b>" + skriv[1] + "<p>");
    document.write( "<p><b>Adre ss: </b>" + skriv[2] + "<p>");
    document.write( "<p><b>Postnumm er: </b>" + skriv[3] + "<p>");
    document.write( "<p><b>Post ort: </b>" + skriv[4] + "<p>");
    document.write( "<p><b>Telefonn ummer: </b>" + skriv[5] + "<p>");
    document.write( "<p><b>E-post: </b>" + skriv[6] + "<p>");
    for (var i=0; i<skriv.length ; i++)
    document.write( "<input type='hidden' name='input'" + i + "' value='" +skriv[i] + "' />");
    }
  • cryme
    New Member
    • Aug 2006
    • 2

    #2
    i fixed it :) just learn how to call a function

    Comment

    • aspcoder
      New Member
      • Oct 2007
      • 1

      #3
      Originally posted by cryme
      Im having a minor, simple problem. Basicly i have two different scripts they both work separately and together but just that both executes at the same time.

      On my html page i have a form, one of the functions are that you can fill in the fields and when you press on a button you go to a new page, were all filled fields are shown.

      Now i want to validate the form before i pass on the filled information to the "next page".

      But right now when you click on the button, you get your form validated as normal and then you just get sent to the "next page" even if you fail to meet up the req.



      JS code:
      Had the same problem. I solved it by checking the name on the inputboxes. If the name in your script and the name on the inputboxes in your form is different, the script will show the error messages on the ones that are correct and the send you directly to your "next" page. If all names are correct it will work perfectly.

      Comment

      Working...