Skip to content

Commit

Permalink
Merge pull request #25 from navedqb/fix/input-validation-int-num
Browse files Browse the repository at this point in the history
fix: numeric key filter validation for dash and dot input
  • Loading branch information
navedqb authored Jan 3, 2025
2 parents 5a976c4 + fdc2ae1 commit dc7690c
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions components/lib/keyfilter/KeyFilter.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,10 @@ export const KeyFilter = {
return;
}

const value = e.target.value;
const regex = this.getRegex(keyfilter);

if (!regex.test(key)) {
e.preventDefault();
}
this.validateSpecialKey(e, key, keyfilter, regex, value)
},

validate(e, keyfilter) {
Expand All @@ -89,5 +88,24 @@ export const KeyFilter = {
}

return validatePattern;
},

validateSpecialKey(e, key, keyfilter, regex, value) {
if (['int', 'num'].includes(keyfilter)) {
const isDash = key === '-';
const isDot = key === '.';

if (
(isDash && (value.includes('-') || value.length > 0)) ||
(keyfilter === 'num' && isDot && value.includes('.')) ||
!regex.test(key)
) {
e.preventDefault();
}
} else {
if (!regex.test(key)) {
e.preventDefault();
}
}
}
};

0 comments on commit dc7690c

Please sign in to comment.