Durga
Durga

Reputation: 565

How to add code for allow only two digits after decimal in my code

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?

Upvotes: 0

Views: 264

Answers (1)

Geee
Geee

Reputation: 2249

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!

Upvotes: 1

Related Questions