-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[NO CHANGELOG] [Add tokens Widget] Fix/add tokens invalid input (#2397)
- Loading branch information
1 parent
769bcb0
commit b8a511e
Showing
2 changed files
with
29 additions
and
27 deletions.
There are no files selected for viewing
11 changes: 5 additions & 6 deletions
11
packages/checkout/widgets-lib/src/widgets/add-tokens/functions/amountValidation.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,15 @@ | ||
const VALID_NUMBER_REGEX = /^[0-9]+(\.[0-9]+)?$/; | ||
const VALID_NUMBER_REGEX = /^(0|[1-9]\d*)(\.\d*)?$/; | ||
|
||
/** | ||
* Validate the amount input | ||
* @param amount - The amount to validate | ||
* @returns An object containing the sanitized value, the float amount, and a boolean indicating if the amount is valid | ||
*/ | ||
export const validateToAmount = (amount: string) => { | ||
const value = amount || '0'; | ||
const value = amount || ''; | ||
const sanitizedValue = value.replace(/^0+(?=\d)/, ''); | ||
const floatAmount = parseFloat(sanitizedValue); | ||
const isValid = VALID_NUMBER_REGEX.test(sanitizedValue); | ||
const floatAmount = isValid ? parseFloat(sanitizedValue) : NaN; | ||
|
||
const isValid = VALID_NUMBER_REGEX.test(sanitizedValue) && floatAmount > 0; | ||
|
||
return { value: sanitizedValue, amount: floatAmount, isValid }; | ||
return { value: sanitizedValue, amount: floatAmount, isValid: isValid && floatAmount > 0 }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters