-
Notifications
You must be signed in to change notification settings - Fork 173
feat: phone number input validation on input and paste events #2906
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
🦋 Changeset detectedLatest commit: 0d67b62 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
|
✅ PR title follows Conventional Commits specification. |
|
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. Latest deployment of this branch, based on commit ff2f0dc:
|
🛡️ Coverage ReportSummaryFull Coverage Details |
| const { key, ctrlKey, metaKey } = event; | ||
|
|
||
| // Check if the entered key is a number; if not, prevent the default action | ||
| const isCharacterKey = key.length === 1; | ||
|
|
||
| // return for non-character keys | ||
| if (ctrlKey || metaKey || !isCharacterKey) { | ||
| return; | ||
| } | ||
|
|
||
| if (!isNumericInput(key)) { | ||
| event.preventDefault(); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| const { key, ctrlKey, metaKey } = event; | |
| // Check if the entered key is a number; if not, prevent the default action | |
| const isCharacterKey = key.length === 1; | |
| // return for non-character keys | |
| if (ctrlKey || metaKey || !isCharacterKey) { | |
| return; | |
| } | |
| if (!isNumericInput(key)) { | |
| event.preventDefault(); | |
| } | |
| const { key, ctrlKey, metaKey } = event; | |
| // return for non-character keys | |
| if (ctrlKey || metaKey || !isNumericInput(key)) { | |
| return; | |
| } | |
| event.preventDefault(); |
Will this do the same thing? isCharacterKey check seems a little hacky
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this should work, will validate once before accepting suggestion.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I missed one aspect in your comment. In case of non-numeric input, we need to prevent default handlers for event, so condition will simply be to return in case of ctrlKey and metaKey.
In case of non-numeric input, we will do event.preventDefault()
| const handlePaste = (pasteEvent: FormInputOnPasteEvent): void => { | ||
| const pastedText = pasteEvent.value?.clipboardData?.getData('text') ?? ''; | ||
|
|
||
| // Only allow pastedText if it only has numeric characters |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not sure if we should stop people from pasting numbers like +91 9999 999999. If you copy from whatsapp or a lot of other websites, it often comes with spaces, -, or +. We can let people paste things as is or we can replace the unsupported characters before pasting and paste the numbers, spaces, hyphens, and plus, etc.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can check the <input type="number" /> behaviour actually. We can replicate that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was thinking about this, the only problem is to understand the amount of characters to skip.
For example, for India, it would be ideal to skip +91 and update only the part after that. Was planning to handle it for current dial code maintained in the state and for any other country code to skip the + sign and paste the remaining part.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah skipping plus and space also fine. Overall its fine if wrongly formatted number is pasted. People can remove the 91 manually. We should just avoid blocking the paste
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Checked what <input type="number" /> does. It strips the string from all non-numeric inputs, adding the same logic here.
Description
This PR adds validation to disallow non-numeric inputs in
PhoneNumberInputcomponent.It also ensures that the same is handled when any text is pasted into the input.
Changes
onPasteevent support forBaseInputcomponent for web viahandleOnPastehandleronKeyDownevent and ensures that onlynumericcharacters are allowedonPasteevent and ensures that text only containsnumbersAdditional Information
diallingCodeandnumber. This is not covered within the scope of this PRComponent Checklist
DevRev Ticket Link