1

I am trying to perform this action like if user choose same value for two different box i have to show some errors.my textbox code as follows.

<input class="order form-control vnumber" type="text" maxlength="1" name="Orderbox[]" required="true">
<input class="order form-control vnumber" type="text" maxlength="1" name="Orderbox[]" required="true">
<input class="order form-control vnumber" type="text" maxlength="1" name="Orderbox[]" required="true">
<input class="order form-control vnumber" type="text" maxlength="1" name="Orderbox[]" required="true">

so the textbox values should be different like 1,2,3,4 it should not be 1,1,1,1 so i have tried real time update using jquery.

$('.order').keyup(function () {       
    // initialize the sum (total price) to zero
    var val = 0;
    var next_val=0;
    // we use jQuery each() to loop through all the textbox with 'price' class
    // and compute the sum for each loop
    $('.order').each(function() {
        val+=$(this).val();
    });
      alert(val);
      if (next_val==val) {
    alert("same value");
      }
      next_val=val;
});

But its not working as i expected can anybody tell is there any solutions for this.Any help would be appreciated.Thank you.

JFIDDLE: jfiddle

3
  • you havent included jquery in your fiddle Commented Jun 6, 2014 at 6:29
  • Also your input tags aren't closed Commented Jun 6, 2014 at 6:34
  • jsfiddle.net/phZaL/6 updated Commented Jun 6, 2014 at 6:35

3 Answers 3

2

Try this Demo Fiddle.

var valarr = [];
$('.order').keyup(function () {

      var curr = $(this).val();
      if (jQuery.inArray(curr, valarr) > -1) {
          alert('exists');
      } else {
          valarr.push(curr);
      }

});

You can use arrays to maintain values. To check the existence of value use inArray()

Sign up to request clarification or add additional context in comments.

Comments

1

You need to put more of the code inside the .each() loop. Also, change val+= to just val=

 $('.order').each(function() {
    val=$(this).val();
    alert(val);
    if (next_val==val) {
          alert("same value");
    }
    next_val=val;
 });

And keep in mind next_val is actually the previous value...

fiddle http://jsfiddle.net/phZaL/8/

Comments

0

This will only work if all values entered till now have the same value
jQuery Code

var arr = [];
$('.order').change(function () {
    arr.push($(this).val());
    if (arr.length > 1) {
        if (arr.AllValuesSame()) alert("Values are same");
    }
    var val = 0;
    $.each(arr, function () {
        val = parseInt(val) + parseInt(this);
    });
    $('.val').text(val);
});
Array.prototype.AllValuesSame = function () {
    if (this.length > 0) {
        for (var i = 1; i < this.length; i++) {
            if (this[i] !== this[0]) return false;
        }
    }
    return true;
}

Demo Fiddle

Made with great help from this answer by @Robert

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.