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 3 of 3
  1. #1
    Regular Coder
    Join Date
    Sep 2008
    Location
    Oklahoma
    Posts
    249
    Thanks
    11
    Thanked 13 Times in 13 Posts

    multiplte OnSubmits in forms

    In my contact form I have JS Submit Once code that is activated by OnSubmit. I have been trying to add JS Validation to check if fields are empty before form is submitted, but it is also activated by OnSubmit.

    How could I use both where the validation is checked first then it goes to the submit once.

    I tried adding th validation code to the beginning of the submit once code. but it didnt work

    edit: this might be in the wrong forum, I had 2 forums windows up and I wasnt looking when I clicked New Thread, please move to html forum if needed.
    Last edited by owt200x; 12-18-2008 at 07:07 PM.

  • #2
    Regular Coder
    Join Date
    Jul 2002
    Location
    Kansas, USA
    Posts
    487
    Thanks
    0
    Thanked 54 Times in 53 Posts
    Post what code you've got and we may be able to help you. (Disregard if this is already being answered in another forum.)

    Typically your onsubmit looks something like this: onsubmit="return doSomething()" where doSomething is a function that returns true if the form is OK and false if not (returning false cancels the submission).

    If you have a second function, doAnother, you can combine these so the form submits only if both functions return true: onsubmit="return doSomething() && doAnother()"

    In this case if either doSomething() or doAnother() returns false the form will not submit.

  • #3
    Regular Coder
    Join Date
    Sep 2008
    Location
    Oklahoma
    Posts
    249
    Thanks
    11
    Thanked 13 Times in 13 Posts
    this is my empty field validation code:
    Code:
    <script language="JavaScript">
    function validateFormOnSubmit(theForm) {
    var reason = "";
     
      reason += validateEmpty(theForm.from);
     
      if (reason != "") {
        alert("Some fields need correction:\n" + reason);
        return false;
      }
     
      return true;
    }
     
    function validateEmpty(fld) {
        var error = "";
     
        if (fld.value.length == 0) {
            fld.style.background = 'Yellow'; 
            error = "The required field has not been filled in.\n"
        } else {
            fld.style.background = 'White';
        }
        return error;   
     </script>
    this is my SubmitOnce code:
    Code:
    <script language="JavaScript">
    //Submit Once form validation- 
    //© Dynamic Drive (www.dynamicdrive.com)
    //For full source code, usage terms, and 100's more DHTML scripts, visit http://dynamicdrive.com
    //if IE 4+ or NS 6+
    function submitonce(theform){
    if (document.all||document.getElementById){
    //screen thru every element in the form, and hunt down "submit" and "reset"
    for (i=0;i<theform.length;i++){
    var tempobj=theform.elements[i]
    if(tempobj.type.toLowerCase()=="submit"||tempobj.type.toLowerCase()=="reset")
    //disable em
    tempobj.disabled=true
    }
    }
    }
    var horizontal_offset="9px"
    var vertical_offset="0"
    var ie=document.all
    var ns6=document.getElementById&&!document.all
    function getposOffset(what, offsettype){
    var totaloffset=(offsettype=="left")? what.offsetLeft : what.offsetTop;
    var parentEl=what.offsetParent;
    while (parentEl!=null){
    totaloffset=(offsettype=="left")? totaloffset+parentEl.offsetLeft : totaloffset+parentEl.offsetTop;
    parentEl=parentEl.offsetParent;
    }
    return totaloffset;
    }
    function iecompattest(){
    return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
    }
    function clearbrowseredge(obj, whichedge){
    var edgeoffset=(whichedge=="rightedge")? parseInt(horizontal_offset)*-1 : parseInt(vertical_offset)*-1
    if (whichedge=="rightedge"){
    var windowedge=ie && !window.opera? iecompattest().scrollLeft+iecompattest().clientWidth-30 : window.pageXOffset+window.innerWidth-40
    dropmenuobj.contentmeasure=dropmenuobj.offsetWidth
    if (windowedge-dropmenuobj.x < dropmenuobj.contentmeasure)
    edgeoffset=dropmenuobj.contentmeasure+obj.offsetWidth+parseInt(horizontal_offset)
    }
    else{
    var windowedge=ie && !window.opera? iecompattest().scrollTop+iecompattest().clientHeight-15 : window.pageYOffset+window.innerHeight-18
    dropmenuobj.contentmeasure=dropmenuobj.offsetHeight
    if (windowedge-dropmenuobj.y < dropmenuobj.contentmeasure)
    edgeoffset=dropmenuobj.contentmeasure-obj.offsetHeight
    }
    return edgeoffset
    }
    // END SUBMIT ONCE FUNCTION
    </script>
    your saying I can combine those in a OnSubmit tag? so the below would work?
    Code:
    <form onSubmit="return validateFormOnSubmit(this) && submitonce(this)">
    Last edited by owt200x; 12-19-2008 at 07:18 AM.


  •  

    Posting Permissions

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