[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [donate/master] Don't allow 0x... numbers in currency amount field



commit 09ca4434469a7356e67d74e1d19b7eb2849b2412
Author: peterh <peterh@xxxxxxxxxxxxxxx>
Date:   Mon Jun 24 11:22:34 2019 -0700

    Don't allow 0x... numbers in currency amount field
    
    Some people are putting I think their wallet ID or something in the
    currency amount field and parseFloat treats that as a hex number. So
    I've switched to a regexp to make it stricter.
    
    I've also added i18n for the error message.
---
 src/js/amount_field.jsx | 7 ++++---
 src/js/number.js        | 4 ++++
 2 files changed, 8 insertions(+), 3 deletions(-)

diff --git a/src/js/amount_field.jsx b/src/js/amount_field.jsx
index 353ee8d8..cfbda302 100644
--- a/src/js/amount_field.jsx
+++ b/src/js/amount_field.jsx
@@ -1,5 +1,7 @@
 import React from 'react';
 import {useState} from 'react';
+import {isFloat} from './number';
+import {t} from './i18n';
 
 export function AmountField(props) {
   const [errorMessage, setErrorMessage] = useState(null);
@@ -7,12 +9,11 @@ export function AmountField(props) {
   const onCurrencyAmountBlur = (event) => {
     const currencyAmount = event.target.value;
     const {name, onFieldValidate} = props;
-    const isNumber = !isNaN(parseFloat(currencyAmount)) && isFinite(currencyAmount);
-    if (isNumber) {
+    if (isFloat(currencyAmount)) {
       setErrorMessage(null);
       onFieldValidate(name, true);
     } else {
-      setErrorMessage('Currency Amount must be a number.');
+      setErrorMessage(t('t-currency-amount-must-be-number'));
       onFieldValidate(name, false);
     }
   };
diff --git a/src/js/number.js b/src/js/number.js
new file mode 100644
index 00000000..03011b88
--- /dev/null
+++ b/src/js/number.js
@@ -0,0 +1,4 @@
+export function isFloat(value) {
+  const matcher = /^\d*\.?\d*$/
+  return matcher.test(value);
+}

_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits