0

I am new to jquery. I have form with two text boxes. In that i am restrict special characters.

Now i want implement decimal values for only two digits.

<form>
 <div class="col-md-6">
<div class="form-group ">
<label for="minAmt" class="col-lg-4 control-label">Min.Amount</label>
<div class="col-lg-6">
 <input type="text" class="form-control" id="minAmt" name="minAmt" placeholder="Enter Min Amount"/>
</div>
</div>
<div class="form-group ">
<label for="maxAmt" class="col-lg-4 control-label">Max.Amount</label>
<div class="col-lg-6">
 <input type="text" class="form-control" id="maxAmt" name="maxAmt" placeholder="Enter Max Amount"/>
</div>
</div>
</div>
</form>

Script code here:

<script>
$('#minAmt').keyup(function(){
var reg = /^0+/gi;
if (this.value.match(reg)) {
  this.value = this.value.replace(reg, '');
}
if (this.value.match(/[^0-9]./g)) {
  this.value = this.value.replace(/[^0-9.]/g, '');
}
});
$('#maxAmt').keyup(function(){
var reg = /^0+/gi;
if (this.value.match(reg)) {
  this.value = this.value.replace(reg, '');
}
if (this.value.match(/[^0-9.]/g)) {
  this.value = this.value.replace(/[^0-9.]/g, '');
}
});

How to implement logic?

Durga
  • 545
  • 7
  • 21
  • 39
  • 1
    Possible duplicate of [Formatting a number with exactly two decimals in JavaScript](http://stackoverflow.com/questions/1726630/formatting-a-number-with-exactly-two-decimals-in-javascript) – mxr7350 Apr 28 '17 at 10:16
  • give some sample values that may help us to understand yours requirement – R.Anandan Apr 28 '17 at 10:25
  • @R.Anandan: `10.05` or `100.50` etc..... – Durga Apr 28 '17 at 10:27

1 Answers1

1

Check this out:

$(function(){
  $('#textbox').on('blur',function(){
    var num = parseFloat($("#textbox").val());
    var new_num = $("#textbox").val(num.toFixed(2));
    
    alert('Only two desimal number shold be acceptable..');return false;
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<input type="text" id="textbox" value="1.1251112314555" />

Here is another demo for on click event. hope this help you!

Geee
  • 2,217
  • 15
  • 30
  • it's not related me. System doesn't allow 3rd digit – Durga Apr 28 '17 at 10:32
  • @Durga, You have mention **"allow only two digits after decimal in my code"** – Geee Apr 28 '17 at 10:38
  • I think you didn't check properly. in on blur while you focus out from the input field value will automatically trim and set only two digits. for example: if you add 92.78787878 after your focus out it will set to 92.78 next all the digits will trimmed.. – Geee Apr 28 '17 at 10:51