Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 274
  • Last Modified:

programmatically submitting a form with validation

Hi,

I have a form, which is sometimes submitted by clicking a submit button, sometimes by enter being pressed in a field, and sometimes programmatically (By the clicking of a "Master Save" elsewhere on the page).

So I created an onsubmit event to do field validation. This works for the first two methods, but breaks for the third. (Ridiculously, the submit() function doesn't trigger onsubmit!)

To hack my way around that, I call the onsubmit event programatically too, but as the form is in an IFrame and the "Master Save" is in the parent page, I can't call the javascript function by name, and have to do this:

if (document.getElementById('IFrame1').contentDocumen t.forms[0].onsubmit())
document.getElementById('IFrame1').contentDocument .forms[0].submit();

This works fine in FF, but IE6 is giving me issues. I know, IE6 is ancient, I have no choice, that's the browser the company mandates.

Can anyone suggest why this is breaking, or how else I can call the function in the IFrame?

Thanks,
Rich
0
Phyrefly
Asked:
Phyrefly
1 Solution
 
bluV11tCommented:
Got the master submit to submit the form but it does not validate
//master save button
<input type=button value="master_submit" onclick="JavaScript=submit_iframe();" />
 
//Javascript function in parent page to call submit of form in iframe
 function submit_iframe(){
     var x = document.getElementById('IFrame1');
 
    if (x.contentWindow) {
    x = x.contentWindow.document.forms[0];
    }
    else {
    x = x.contentDocument.forms[0];
    }
    return(x.submit());
 }

Open in new window

0
 
PhyreflyAuthor Commented:
Thank you, that iis what I was missing, it works now.

To make it validate as well, I also had to do this in the IFrame itself:

      var f = document.getElementById('DetailsForm');
      f._submit = f.submit;
      f.submit = function() { if (SanityCheck()) { this._submit() } };

Rich
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now