Validation of JS forms



  • When the form is validated, when all fields checked in selInputs(s) are filled are alerted twice. What's the problem?

    var newsForm = $('#add_news_form'),
      themesForm = $('#add_theme_form');
    

    function selInputs(arg) {
    var title = arg.children('.add_mat_title').find('.text'),
    shortDesc = arg.children('.add_mat_short').find('.text'),
    text = arg.children('.add_mat_text').find('.text'),
    source = arg.children('.add_mat_source').find('.text'),
    tags = arg.children('.add_mat_tags').find('.text'),
    inputs = [];

    inputs.push(title, shortDesc, text, source, tags);

    for (var i = 0; i < inputs.length; i++) {
    if (inputs[i].val() == null || inputs[i].val() == '') {
    return false;
    }
    }

    return true;
    }

    function formValidation(form) {
    $(this).submit(function() {
    if (selInputs($(this)) === false) {
    alert('Заполните все поля!')
    return false;
    }
    });
    }

    formValidation(newsForm);
    formValidation(themesForm);



  • So you've got a function called twice. And it turns out that if in both forms selInputs($(this)) It's false, it's double alert. Try adding to the arguments of each form is: onsubmit="formValidation(this)" and remove all the challenges of formValidation below the crypt.




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2