Skip to content

Commit

Permalink
Merge pull request #5024 from mananjadhav/fix/amount-comma-regex
Browse files Browse the repository at this point in the history
Allow pasting amount with comma
  • Loading branch information
marcaaron authored Sep 9, 2021
2 parents ddd7f92 + 0dfd79d commit 5e401d5
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions src/pages/iou/steps/IOUAmountPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ class IOUAmountPage extends React.Component {

this.updateAmountNumberPad = this.updateAmountNumberPad.bind(this);
this.updateAmount = this.updateAmount.bind(this);
this.stripCommaFromAmount = this.stripCommaFromAmount.bind(this);

this.state = {
amount: props.selectedAmount,
};
Expand All @@ -98,10 +100,20 @@ class IOUAmountPage extends React.Component {
* @returns {Boolean}
*/
validateAmount(amount) {
const decimalNumberRegex = new RegExp(/^\d+(\.\d{0,3})?$/, 'i');
const decimalNumberRegex = new RegExp(/^\d+(,\d+)*(\.\d{0,3})?$/, 'i');
return amount === '' || decimalNumberRegex.test(amount);
}

/**
* Strip comma from the amount
*
* @param {String} amount
* @returns {String}
*/
stripCommaFromAmount(amount) {
return amount.replace(/,/g, '');
}

/**
* Update amount with number or Backspace pressed for BigNumberPad.
* Validate new amount with decimal number regex up to 6 digits and 2 decimal digit to enable Next button
Expand All @@ -121,7 +133,7 @@ class IOUAmountPage extends React.Component {

this.setState((prevState) => {
const amount = `${prevState.amount}${key}`;
return this.validateAmount(amount) ? {amount} : prevState;
return this.validateAmount(amount) ? {amount: this.stripCommaFromAmount(amount)} : prevState;
});
}

Expand All @@ -133,7 +145,7 @@ class IOUAmountPage extends React.Component {
*/
updateAmount(amount) {
if (this.validateAmount(amount)) {
this.setState({amount});
this.setState({amount: this.stripCommaFromAmount(amount)});
}
}

Expand Down

0 comments on commit 5e401d5

Please sign in to comment.