Click here to Skip to main content
15,918,333 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
<pre lang="HTML">
<input type="text" class="form-control" name="component_gst_percent" id="component_gst_percent{{@$capexItem->po_item_config_id}}" value="{{@$capexItem->component_gst_percent}}" placeholder="Enter GST"  onkeydown="javascript: return event.keyCode == 69 ? false : true" oninput="this.value = this.value.replace(/[^0-9.]/g, '').replace(/(\..*?)\..*/g, '$1').replace(/(\.\d{2}).+/g, '$1');">

Oninput it is matching 2 digit after decimal point not before. help me with it thanks

What I have tried:

I tried this code but not preventing entering input and pattern tag is not working
function checkInput(data){
              let gst_input=data.value;
              let regex=new RegExp("^\\d{0,2}$|(?=^.{0,5}$)^\\d+\\.\\d{0,2}$");
              if (!regex.test(gst_input)) {
                  return false;
              return true;
Chris Copeland 31-Mar-22 4:19am    
Can I ask exactly what decimal formats you're trying to match/validate against? Your regular expression pattern doesn't make much sense to me, specifically: ^.{0,5}$ which is "any text between 0 and 5 characters in length". Can you make it clearer what sort of decimal formats you're expecting to be supported? \\d{0,2} also indicates you only want to support whole numbers between 0 and 2 digits in length?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900