6

Check all the input text fields in the blank form or not on Submit, Ignore if at...

 3 years ago
source link: https://www.codesd.com/item/check-all-the-input-text-fields-in-the-blank-form-or-not-on-submit-ignore-if-at-least-one-text-field-is-filled.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Check all the input text fields in the blank form or not on Submit, Ignore if at least one text field is filled

advertisements

Here is the situation

I have a form with many input fields in my JSP page. The number of input text fields are unknown as it is based on the previous action class.

When the form is submitting, I want to check all input text fields are empty or not. If all are empty, then it should not submit. Otherwise( in any case. ie, at least one text field must be filled), then form should submit.

The JQuery I have tried:

$('#dataFieldMapping').submit(function() {
    if ($('input:text').is(":empty")) {
        alert("Empty!");
        preventDefault();
     }
});

(dataFieldMapping is the id of form). When I click on submit, it alerts with "Empty!" always, even if all text fields are filled.

NB: My submit button is outside of form. So I was using

$('#dataFieldMapping').submit();

without validating. But I want to validate the form.


DEMO

$('#dataFieldMapping').submit(function (e) {
    if (!$(this).find('input:text').filter(function () {
        return $.trim(this.value) != ""
    }).length) {
        alert("Empty!");
        e.preventDefault();
    }
});


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK