Skip to main content
added 198 characters in body; edited tags; edited title; deleted 67 characters in body; edited title
Source Link
200_success
  • 144.2k
  • 22
  • 188
  • 473

How can I remove duplicate error alert code from my jquery jQuery function? to display an alert when submitting blank fields

function before_submit_check_if_has_blank_field() {
    $("[type=submit]").click(function(e) {
        var of_key_field = $('.key_field')
        var of_val_field = $('.val_field')
        var should_run = 1

        if (of_key_field.length === 1) {
            $('.key_field:not(:first)').filter(function() {
                if (this.value === "") {
                    alert('Please Fill In Or Remove The Blank Fields First')
                    e.preventDefault();
                    should_run = 0
                }
            });
        } else {
            $('.key_field').filter(function() {
                if (this.value === "") {
                    alert('Please Fill In Or Remove The Blank Fields First')
                    e.preventDefault();
                    should_run = 0
                }
            });
        }

        if (should_run != 0) {
            if (of_val_field.length === 1) {
                $('.val_field:not(:first)').filter(function() {
                    if (this.value === "") {
                        alert('Please Fill In Or Remove The Blank Fields First')
                        e.preventDefault();
                        should_run = 0
                    }
                });
            } else {
                $('.val_field').filter(function() {
                    if (this.value === "") {
                        alert('Please Fill In Or Remove The Blank Fields First')
                        e.preventDefault();
                        should_run = 0
                    }
                });
            }
        }

        update_config_field_from_key_value_field()
    });
}

}

The above code snippet is a part of my jquery where I am displaying a layout to the user with some fields and once those fields are empty then I am showing alert message while saving the form.

if (this.value === "") {
                    alert('Please Fill In Or Remove The Blank Fields First')
                    e.preventDefault();
                    should_run = 0
                }
if (this.value === "") {
    alert('Please Fill In Or Remove The Blank Fields First')
    e.preventDefault();
    should_run = 0
}

How can I remove duplicate error alert code from my jquery function?

function before_submit_check_if_has_blank_field() {
$("[type=submit]").click(function(e) {
    var of_key_field = $('.key_field')
    var of_val_field = $('.val_field')
    var should_run = 1

    if (of_key_field.length === 1) {
        $('.key_field:not(:first)').filter(function() {
            if (this.value === "") {
                alert('Please Fill In Or Remove The Blank Fields First')
                e.preventDefault();
                should_run = 0
            }
        });
    } else {
        $('.key_field').filter(function() {
            if (this.value === "") {
                alert('Please Fill In Or Remove The Blank Fields First')
                e.preventDefault();
                should_run = 0
            }
        });
    }

    if (should_run != 0) {
        if (of_val_field.length === 1) {
            $('.val_field:not(:first)').filter(function() {
                if (this.value === "") {
                    alert('Please Fill In Or Remove The Blank Fields First')
                    e.preventDefault();
                    should_run = 0
                }
            });
        } else {
            $('.val_field').filter(function() {
                if (this.value === "") {
                    alert('Please Fill In Or Remove The Blank Fields First')
                    e.preventDefault();
                    should_run = 0
                }
            });
        }
    }

    update_config_field_from_key_value_field()
});

}

The above code snippet is a part of my jquery where I am displaying a layout to the user with some fields and once those fields are empty then I am showing alert message while saving the form.

if (this.value === "") {
                    alert('Please Fill In Or Remove The Blank Fields First')
                    e.preventDefault();
                    should_run = 0
                }

jQuery function to display an alert when submitting blank fields

function before_submit_check_if_has_blank_field() {
    $("[type=submit]").click(function(e) {
        var of_key_field = $('.key_field')
        var of_val_field = $('.val_field')
        var should_run = 1

        if (of_key_field.length === 1) {
            $('.key_field:not(:first)').filter(function() {
                if (this.value === "") {
                    alert('Please Fill In Or Remove The Blank Fields First')
                    e.preventDefault();
                    should_run = 0
                }
            });
        } else {
            $('.key_field').filter(function() {
                if (this.value === "") {
                    alert('Please Fill In Or Remove The Blank Fields First')
                    e.preventDefault();
                    should_run = 0
                }
            });
        }

        if (should_run != 0) {
            if (of_val_field.length === 1) {
                $('.val_field:not(:first)').filter(function() {
                    if (this.value === "") {
                        alert('Please Fill In Or Remove The Blank Fields First')
                        e.preventDefault();
                        should_run = 0
                    }
                });
            } else {
                $('.val_field').filter(function() {
                    if (this.value === "") {
                        alert('Please Fill In Or Remove The Blank Fields First')
                        e.preventDefault();
                        should_run = 0
                    }
                });
            }
        }

        update_config_field_from_key_value_field()
    });
}

The above code snippet is a part of my jquery where I am displaying a layout to the user with some fields and once those fields are empty then I am showing alert message while saving the form.

if (this.value === "") {
    alert('Please Fill In Or Remove The Blank Fields First')
    e.preventDefault();
    should_run = 0
}
added summary of my function
Source Link

HereThe above code snippet is a part of my jquery where I am displaying a layout to the user with some fields and once those fields are empty then I am showing alert message while saving the form.

In above function I am repeating following lines of alert message which must be replaced by standard one-

Here I am repeating following lines which must be replaced by standard one-

The above code snippet is a part of my jquery where I am displaying a layout to the user with some fields and once those fields are empty then I am showing alert message while saving the form.

In above function I am repeating following lines of alert message which must be replaced by standard one-

Source Link

How can I remove duplicate error alert code from my jquery function?

I am new to jquery and implemented following function in a js file in case of error-

function before_submit_check_if_has_blank_field() {
$("[type=submit]").click(function(e) {
    var of_key_field = $('.key_field')
    var of_val_field = $('.val_field')
    var should_run = 1

    if (of_key_field.length === 1) {
        $('.key_field:not(:first)').filter(function() {
            if (this.value === "") {
                alert('Please Fill In Or Remove The Blank Fields First')
                e.preventDefault();
                should_run = 0
            }
        });
    } else {
        $('.key_field').filter(function() {
            if (this.value === "") {
                alert('Please Fill In Or Remove The Blank Fields First')
                e.preventDefault();
                should_run = 0
            }
        });
    }

    if (should_run != 0) {
        if (of_val_field.length === 1) {
            $('.val_field:not(:first)').filter(function() {
                if (this.value === "") {
                    alert('Please Fill In Or Remove The Blank Fields First')
                    e.preventDefault();
                    should_run = 0
                }
            });
        } else {
            $('.val_field').filter(function() {
                if (this.value === "") {
                    alert('Please Fill In Or Remove The Blank Fields First')
                    e.preventDefault();
                    should_run = 0
                }
            });
        }
    }

    update_config_field_from_key_value_field()
});

}

Here I am repeating following lines which must be replaced by standard one-

if (this.value === "") {
                    alert('Please Fill In Or Remove The Blank Fields First')
                    e.preventDefault();
                    should_run = 0
                }

Can anyone please review my code and refactor it?