I am new to jquery. I have a form with two input boxes. In that i am implemented some validations.
<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 minAmt" 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 maxAmt" id="maxAmt" name="maxAmt" placeholder="Enter Max Amount" />
</div>
</div>
</div>
<div class="col-md-12">
<div class="form-actions btnzone">
<button type="button" class="btn btn-success savebtn" style="padding: 6px 12px;margin-left: 40%;" id="addbutton" ><i class="icon-check-sign" aria-hidden="false"></i>Add</button>
</div>
</div>
</form>
Script code here:
$('#minAmt,#maxAmt').keyup(function(){
// if($(this).val() == '0'){
//$(this).val('');
//}
var val = this.value;
var re = /^([0-9]+[\.]?[0-9]?[0-9]?|[0-9]+)$/g;
var re1 = /^([0-9]+[\.]?[0-9]?[0-9]?|[0-9]+)/g;
if (re.test(val)) {
//do something here
} else {
val = re1.exec(val);
if (val) {
this.value = val[0];
} else {
this.value = "";
}
}
});
Here i am allowing first value 0
and decimal value allowing up to two
points only.
But here some cases wrong input's taking. For Example i enter 0500
or 0000555
etc.. it allowing starting with 0
,that's why previously i restricted 0
doesn't allow first character but it is effected to decimal values starts with 0
(0.01
or 0.98
) are not allowing.
My requirement is money
related. So i want change to 0000555
to 555
.
How to modify my code?