开发者

Formvalidator from the iframe

开发者 https://www.devze.com 2022-12-31 09:30 出处:网络
Hello I have a following formvalidatior function in my document. function formValidator(formid) { var form = cic.$(formid);

Hello I have a following formvalidatior function in my document.

function formValidator(formid) {

  var form = cic.$(formid);

  if(!form) return ('');

  var errors = [];
    var len = form.elements.length;

  for(var elementIdx = 0; elementIdx < len; elementIdx++) {

    var element = form.elements[elementIdx];

    if(!element && !element.getAttribute('validationtype')) return ('');

    switch (element.getAttribute('validationtype')) {

      case 'text'    :  if(cic.getValue(element).strip() == "") errors.push(element.getAttribute('validationmsg'));
                        break;

      case 'email'   :  if(!cic.isEmail(cic.getValue(element))) errors.push(element.getAttribute('validationmsg'));
                        break;

      case 'numeric' :  if(isNaN(cic.getValue(element).replace(',', '.'))) errors.push(element.getAttribute('validationmsg'));
                        break;         

      case 'confirm' :  if(cic.getValue(cic.$(element.getAttribute('sourcefield'))) !== cic.getValue(element)) errors.push(element.getAttribute('validationmsg'));
                        break;         
    }

  }

  return (errors.length > 0) ? '<li>' + errors.uniq().join("<li>") : '';
}

It works fine, now I have an Iframe in my 开发者_开发知识库document, and that I frame contains the form to validate. What will be the best practice to change this function in such a way that it can validate document forms and iframe from simeltaniously.

Thanks


Instead of passing the ID of the form, pass it the form element (i.e. do the lookup outside the function).

Then write two functions: One which iterates of the forms of a document (pass the document as a parameter) and calls formValidator(). Use the document.forms array to locate the forms.

Plus a second one which iterates over all iframes and calls the first function. Use the document.iframes array and then frame.document to get the document of the iframe.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号