Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Input field value should not be greater than other field value

I have two input fields one for advance payment and other for full payment, which are fetched from the database in array.

If onchange or keyup the advance payment is greater than full payment, Then the advance payment should not be entered or should be equal to full payment.

I am trying with this code

<input type="text" value="<?php echo $full_payemnt; ?>" id="full_payment_<?php echo $i; ?>">
<input type="text" id="advance_payment_<?php echo $i; ?>" class="advance">
$('.advance').on('change keyup blur', function(e){ 
  var fullPay = $('#full_payment_'+id[1]).val();
  var advancePay = $('#advance_payment_'+id[1]).val();
  console.log( parseInt(advancePay) > parseInt(fullPay))
    if (parseInt(advancePay ) > parseInt(fullPay)) {
       e.preventDefault();     
       $('#advance_payment_'+id[1]).val(fullPay);
    }
});
like image 389
Mooni Avatar asked Jun 01 '18 15:06

Mooni


People also ask

How to display a message for field input that is not valid?

Create a message to display for field input that is not valid 1 Select the field that needs a message for input that is not valid. The field should already have a validation rule. 2 On the Fields tab, in the Field Validation group, click Validation, and then click Field Validation Message. 3 Enter an appropriate message. ...

How to validate an input value against another input value?

A typical requirement is to validate an input value against another another input value on a screen. A classic example is a form with two input controls: Start date and End date. In this situation, we have to ensure that End Date is greater than or equal to Start Date.

How do I restrict input to a specific field?

Field properties   Some field properties restrict data input. For example, the Field Sizeproperty of a field restricts input by limiting the amount of data. You can also use the Validation Ruleproperty to require specific values, and the Validation Textproperty to alert your users to any mistakes.

Can the validation number be greater than 0?

The validation number should be greater than 0. I am not submitting the form to the SharePoint list, if the line item quantity is 0. I want to visually prompt the user that they need to add quantity value greater than 0. Let me know so I can guide you with the best possible suggestion.


2 Answers

Here is an example of how you can do it.

var $full = $('[name=full]');
var $adv = $('[name=adv]');

$adv.on('keyup keydown', function(e) {
  var fullPay = parseInt($full.val(), 10);
  var advPay = parseInt($adv.val(), 10); //tell the parser it is base 10

  if (advPay > fullPay &&
    e.keyCode !== 46 // keycode for delete
    &&
    e.keyCode !== 8 // keycode for backspace
  ) {
    e.preventDefault();
    $adv.val('');
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="number" name="full" />
<input type="number" name="adv" />
like image 147
Sam Battat Avatar answered Nov 14 '22 22:11

Sam Battat


Try this, I think you want this.

$('.advance').on('change keyup blur', function(e){ 
  id_arr = $(this).attr('id');
	id = id_arr.split("_");
  var fullPay = $('#fullPayment_'+id[1]).val();
  var advancePay = $('#advancePayment_'+id[1]).val();
    if ($(this).val() > parseInt(fullPay)) {
       e.preventDefault();     
       $(this).val(fullPay);
    }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<tr>
  <input type="text" value="12" id="fullPayment_1">
  <input type="text" id="advancePayment_1" class="advance">
</tr>
<br>
<tr>
  <input type="text" value="19" id="fullPayment_2">
  <input type="text" id="advancePayment_2" class="advance">
</tr>
like image 27
mishor Avatar answered Nov 14 '22 21:11

mishor