Browse Source

remove letters from money mask (#3174)

* remove letters from money mask

* Update to use regex test

Co-authored-by: Josh Hanley <josh@hitsystems.com.au>
Pedro Oliveira 2 years ago
parent
commit
1038203aaf
2 changed files with 13 additions and 0 deletions
  1. 2 0
      packages/mask/src/index.js
  2. 11 0
      tests/cypress/integration/plugins/mask.spec.js

+ 2 - 0
packages/mask/src/index.js

@@ -164,6 +164,8 @@ export function buildUp(template, input) {
 }
 }
 
 
 export function formatMoney(input, delimiter = '.', thousands) {
 export function formatMoney(input, delimiter = '.', thousands) {
+    if (/^\D+$/.test(input)) return '9'
+
     thousands = thousands ?? (delimiter === "," ? "." : ",")
     thousands = thousands ?? (delimiter === "," ? "." : ",")
 
 
     let addThousands = (input, thousands) => {
     let addThousands = (input, thousands) => {

+ 11 - 0
tests/cypress/integration/plugins/mask.spec.js

@@ -167,3 +167,14 @@ test('$money works with permenant inserted at beginning',
         get('input').should(haveValue('40.00'))
         get('input').should(haveValue('40.00'))
     }
     }
 )
 )
+
+test('$money mask should remove letters or non numeric characters',
+    [html`<input x-data x-mask:dynamic="$money">`],
+    ({ get }) => {
+        get('input').type('A').should(haveValue(''))
+        get('input').type('ABC').should(haveValue(''))
+        get('input').type('$').should(haveValue(''))
+        get('input').type('/').should(haveValue(''))
+        get('input').type('40').should(haveValue('40'))
+    }
+)