user1048676
user1048676

Reputation: 10066

Verify that all text inputs have a value in them and that none are the same

I've got the following code which works checks the other values to ensure none are the same but how can I also verify every text input also have a value in it:

$( "#save_sort_order_board_members" ).click(function(e) {
    $('#sort_error_text').html('');
    var failed;
    var inputs = {};
      $('.sort_member_order_text').each(function() {
        if (inputs[this.value] !== undefined) {
          // a previous element with the same value exists
          // apply class to both elements
          //$([this, inputs[this.value]]).addClass('same');
          $('#sort_error_text').html('You need to have different values to sumbit!');
          failed = "yes";
        }
        inputs[this.value] = this;
      });
      if(failed!="yes"){
        $('#sort_board_member_field_values').submit();
      }
});

Here is a JS Fiddle: http://jsfiddle.net/0o1ze3wu/

Upvotes: 0

Views: 60

Answers (3)

Tushar
Tushar

Reputation: 87203

You need to trim the values:

Remove the whitespace from the beginning and end of a string.

$(document).ready(function () {
    $("#save_sort_order_board_members").click(function (e) {
        $('#sort_error_text').html('');

        var failed;
        var inputs = {};
        $('#sort_board_member_field_values input[type="text"]').each(function () {
            var thisVal = $.trim($(this).val());
            if (!thisVal) {
                alert('All fields are compulsory');
                failed = true;
            } else if (inputs[thisVal]) {
                alert('All inputs should have different values');
                failed = true;
            }
            inputs[thisVal] = true;
        });

        if (failed) {
            return false;
        }
        $('#sort_board_member_field_values').submit();
    });
});

Docs: http://api.jquery.com/jQuery.trim/

Demo: http://jsfiddle.net/0o1ze3wu/2/

Upvotes: 1

user1048676
user1048676

Reputation: 10066

With a little help from @Tushar I was able to solve it:

$( "#save_sort_order_board_members" ).click(function(e) {
    $('#sort_error_text').html('');
    var failed;
    var inputs = {};
    $('.sort_member_order_text').each(function () {
        if ($.trim($(this).val())==='' || inputs[this.value] !== undefined) {
            $('#sort_error_text').html('You need to have different values to sumbit!');
            failed = "yes";
            alert('failed');
        }
        inputs[this.value] = this;
    });
    if (failed != "yes") {
        alert('success');
        return false;
        $('#sort_board_member_field_values').submit();
    }
});

Upvotes: 0

Milind Anantwar
Milind Anantwar

Reputation: 82241

You can use:

if($('.sort_member_order_text').length != $(".sort_member_order_text").filter(function() {return this.value.length !== 0;})){
   alert('all input are mandatory');
   return false;
}
var values = [];
$('.sort_member_order_text]').each(function() {
   if ( $.inArray(this.value, values) >= 0 ) {
     alert("all input values be unique.");
     return false; //break the loop
} else {
    values.push( this.value );
}});

Upvotes: 0

Related Questions