error check before form submission

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • cssExp
    New Member
    • Jun 2007
    • 59

    error check before form submission

    Hello,

    It should be working but for some reasons it's not.

    [code="JavaScrip t"]function Validator(frmna me)
    {
    this.formobj=do cument.forms[frmname];
    if(!this.formob j)
    {
    alert("BUG: couldnot get Form object "+frmname);
    return;
    }
    if(this.formobj .onsubmit)
    {
    this.formobj.ol d_onsubmit = this.formobj.on submit;
    this.formobj.on submit=null;
    }
    else
    {
    this.formobj.ol d_onsubmit = null;
    }
    this.formobj.on submit=form_sub mit_handler;
    this.setAddnlVa lidationFunctio n=set_addnl_vfu nction;
    this.clearAllVa lidations = clear_all_valid ations;
    }

    function set_addnl_vfunc tion(functionna me)
    {
    this.formobj.ad dnlvalidation = functionname;
    }

    function clear_all_valid ations()
    {
    for(var itr=0;itr < this.formobj.el ements.length;i tr++)
    {
    this.formobj.el ements[itr].validationset = null;
    }
    }

    function form_submit_han dler()
    {
    for(var itr=0;itr < this.elements.l ength;itr++)
    {
    if(this.element s[itr].validationset &&
    !this.elements[itr].validationset. validate())
    {
    return false;
    }
    }
    if(this.addnlva lidation)
    {
    str =" var ret = "+this.addnlval idation+"()";
    eval(str);
    if(!ret) return ret;
    }
    return true;
    }

    function uploaderError()
    {
    var frm = document.forms["upload"];

    var uplSplit = frm.filePath.va lue.match("^(.+ ).(.+)$");
    if (uplSplit == null) { ErrorPath(); return false; }

    if (uplSplit[2] != null )
    {
    if ((uplSplit[2].match("gif") == null) || (uplSplit[2].match("jpg") == null) || (uplSplit[2].match("jpg") == null))
    { ErrorPath(); return false; }
    else { return true; }
    }


    if (frm.filePath.v alue == '')
    {
    ErrorPath();
    return false;
    }
    else if ((frm.Title.val ue == '') || (strlen(frm.Tit le.value) > 15))
    {
    ErrorTitle();
    return false;
    }
    else if ((frm.height.va lue != '') && (frm.width.valu e == ''))
    {
    Errorresize();
    return false;
    }
    else if ((frm.hieght.va lue != '') && (frm.width.valu e == ''))
    {
    Errorresize();
    return false;
    }
    else if ((frm.pincode.v alue != '') && (frm.memid.valu e == ''))
    {
    ErrorMem();
    return false;
    }
    else if ((frm.memid.val ue != '') && (frm.pincode.va lue == ''))
    {
    ErrorMem();
    return false;
    }
    else
    {
    return true;
    }
    }[/code]

    [code="JavaScrip t"]<form action="process .php" method="post" name="upload">[/code]

    outside form

    [code="HTML4Stri ct"]</form>
    <script language="JavaS cript" type="text/javascript">
    var frmCheck = new Validator("uplo ad");
    frmCheck.setAdd nlValidationFun ction("uploader Error");
    </script>[/code]

    form name is "upload". The problem is, it's not going through the checks.

    Thanks in Advace.
  • cssExp
    New Member
    • Jun 2007
    • 59

    #2
    could anyone here help? ...is it that impossible to do error check on submit?

    Comment

    • acoder
      Recognized Expert MVP
      • Nov 2006
      • 16032

      #3
      onsubmit must return true or false.

      Do you get any errors?

      Comment

      • cssExp
        New Member
        • Jun 2007
        • 59

        #4
        Originally posted by acoder
        onsubmit must return true or false.

        Do you get any errors?
        the problem is it's not going through the checks.. even when i put onSubmit in form directly in the following way.

        [CODE="javacrpt"]function eCheck()
        {
        var filePath = getElementbyId( 'filePath');

        if (filePath == '')
        {
        alert('This is not valid');
        return false;
        }
        else
        {
        return true;
        }
        } [/CODE]

        and
        [CODE="html"]<form action="process .php" method="post" onSubmit="retur n eChek()">[/CODE]

        above is not related to the mentioned script. but it shows that its not going through 'if'. Don't know why.

        Comment

        • gits
          Recognized Expert Moderator Expert
          • May 2007
          • 5388

          #5
          Code:
          getElementById()
          is case senstive

          Comment

          • cssExp
            New Member
            • Jun 2007
            • 59

            #6
            Originally posted by gits
            Code:
            getElementById()
            is case senstive
            yes i know that, i mistyped it here. i did use getElementById

            Comment

            • gits
              Recognized Expert Moderator Expert
              • May 2007
              • 5388

              #7
              aaarrgh ... i missed that simple mistake you made ;) sorry ... you have to compare the value of course:

              [CODE=javascript]var filePath = document.getEle mentById('fileP ath').value;[/CODE]

              kind regards ...

              Comment

              • cssExp
                New Member
                • Jun 2007
                • 59

                #8
                great! it works!, but why doesn't the script that my twin wrote not working (one in the main post)

                Comment

                • gits
                  Recognized Expert Moderator Expert
                  • May 2007
                  • 5388

                  #9
                  hi ...

                  i bet that is the typical problem with doing script-things during load ... ;) i always recommend to start all javascript-activities that refer to page-elements with the onload-handler of the document's body. during load the document dom isn't reliable ready ... onload it is ... try this and i think it should work that way ;))

                  kind regards

                  Comment

                  Working...