简体   繁体   中英

format text input in currency pattern using regex

I have text input which when entered in formats it in currency pattern like 1,000,500 . The problem right now is that a user can enter numbers like 000.00, 0,0000,000, 000,438,339 .

I want to find out if someone can help me with a regex that removes the starting zeros except it is in this format 0.00 .

If I entered more than one zero without placing the decimal point after the first zero eg 00 or 0000 then it should return 0. So :

000.00 should be 0.00
0,0000,000 should be 0
000,438,339 should be 438,339

I was doing this '0000.00'.replace(/^(00*)(.*)$/, '$2') but id doesn't cover all the edge cases.

You can use /^0+(\\d+(\\.\\d{1,2})?)$/ after removing commas from the string; This covers the three cases you've listed:

'0,0000,000'.replace(/,/g, "").replace(/^0+(\d+(\.\d{1,2})?)$/, '$1')
// '0'

'000.00'.replace(/,/g, "").replace(/^0+(\d+(\.\d{1,2})?)$/, '$1')
// '0.00'

'000,438,339'.replace(/,/g, "").replace(/^0+(\d+(\.\d{1,2})?)$/, '$1')
// '438339'

Check the following code.The general idea is to check first if there is a dot notation in the input value. Then split the value at dot position.Via if condition i check if the first portion of the number (00. or 000. or 10.) is divided with number 1 and then the result is not equal to 0.This ensure that the integer portion is not zero's.After this if conditional I reform the value using toLocaleString() method for the first portion of the initial input number and reset the input value:

 $('#myButton').on('click',function(){ var currency=$('#myInput').val(); var search=currency.indexOf("."); if(search){ currency=currency.split("."); if((currency[0]/1)==0){ currency[0]="0"; var newNum=currency[1].replace(/\\B(?=(\\d{3})+(?!\\d))/g, ","); $('#myInput').val(currency[0]+"."+newNum); }else{ var newNum=currency[1].replace(/\\B(?=(\\d{3})+(?!\\d))/g, ","); $('#myInput').val(Math.round(parseInt(currency[0]))+"."+newNum); } } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="text" id="myInput" /> <button id="myButton">Get Currency</button> 

The commented snippet below illustrates effectively what you want to achieve; the function formats your given value to the desired format.

 // Defining input cases variable. var caseOne = '000.00'; var caseTwo = '00,0000,000'; var caseThree = '000,438,339'; // Assigning a given case to the someAmount variable (any of the cases above). // For the purpose of this illustration, we assigned caseThree as value to someAmount. var someAmount = caseThree; // formatInput function (to format the given input case). function formatInput(amountToFormat) { if (amountToFormat.match(/^0*(\\.)(0*)$/)) { console.log('0'.concat(amountToFormat.replace(/^0*(\\.)(0*)$/, '$1' + '$2'))); } if (amountToFormat.match(/^0*(\\,)([0-9]+)(\\,)([0-9]+)$/)) { var blockTwo = amountToFormat.replace(/^0*(\\,)(0*)(\\,)(0*)$/, '$2'); var blockFour = amountToFormat.replace(/^0*(\\,)(0*)(\\,)(0*)$/, '$4'); if (eval(blockTwo) != 0 && eval(blockFour) != 0) { console.log(amountToFormat.replace(/^0*(\\,)([0-9]+)(\\,)([0-9]+)$/, '$2' + '$3' + '$4')); } else { console.log('0'); } } } // Use the formatInput function where needed by passing in the value // of the input to be formatted as someAmount. // Expected outputs: // 0.00 for caseOne, 0 for caseTwo, and 438,339 for caseThree. formatInput(someAmount); // Expected output for the purpose of this illustration: 438,339 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM