JSFiddle - React, Tailwind, and code Playground

by BenjaminRay

HTML

<form id="form1">
    <input type="text" value="something" />
    <input id="submit1" type="submit" value="send" />
    <input id="submit2" type="submit" value="ignore" />
</form>

JavaScript

$('#submit2').on('click', function (event) {
        event.preventDefault();
        // Form will not be submitted
        // Do whatever you need to do when this button is clicked
    });

    $('form').on('submit', function () {
        var thisForm = $(this);
        var thisAlert = thisForm.data('alert');
        var canSubmit = true;
        thisForm.find('[data-alert]').each(function(i) {
            var thisInput = $(this);
            if ( !$.trim(thisInput.val()) ) {
                thisAlert += '\n' + thisInput.data('alert');
                canSubmit = false;
            };
        });
        if( !canSubmit ) {
            alert( thisAlert );
            return false;
        }
    });