Skip to content
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

Prevent non-numeric input in contesting-serial fields #3023

Merged
merged 1 commit into from
Apr 2, 2024

Conversation

DJ3CE
Copy link
Contributor

@DJ3CE DJ3CE commented Mar 23, 2024

Firefox allows non-numeric input in type=number-inputs. This PR adds/modifies code to prevent this by checking the codepoint of the keyboardEvent.

@patrickrb
Copy link
Contributor

Your code is generally correct and will prevent non-numeric input. However, there are a few potential issues to consider:

Accessibility: Some users might use the keyboard to navigate, and your current code would prevent the use of keys like Tab, Backspace, and Arrow keys. You might want to allow these keys.

Copy and Paste: Users can still paste non-numeric characters into the input field. If you want to prevent this, you'll need to handle the paste event.

Non-Standard Keys: Some keyboards might have non-standard keys that produce a character code less than 48 or greater than 57. This is rare, but it's something to keep in mind.

$('#exch_serial_r, #exch_serial_s').on('keypress', function (e) {
    var charCode = (e.which) ? e.which : e.keyCode;
    if (!(/[0-9]/.test(String.fromCharCode(charCode))) && 
        !(charCode == 8 || charCode == 9 || charCode == 37 || charCode == 39)) {
        return false;
    }
}).on('paste', function (e) {
    var pasteData = e.originalEvent.clipboardData.getData('text');
    if (pasteData.match(/[^0-9]/)) {
        e.preventDefault();
    }
});

This version allows the Tab (9), Backspace (8), Left Arrow (37), and Right Arrow (39) keys. It also prevents pasting non-numeric characters.

@DJ3CE
Copy link
Contributor Author

DJ3CE commented Mar 27, 2024

Thanks for putting focus on accessibility, although all mentioned keys and pasting did work in Firefox as well as in Chrome in my tests. Which browser did you use?

The numpad-keys e.g. may have different keyCodes and that's why the code does not test on keyCode or which, but on the UTF-16 codepoint given by charCodeAt of key.

@patrickrb
Copy link
Contributor

It was just pseudo code again, didn't test it. I'll see if I can get it working today

@patrickrb
Copy link
Contributor

patrickrb commented Mar 27, 2024

Doh! i thought this PR was changing the exchange not the serial. It looks like the serial inputs are both number type and shouldn't allow alpha characters on modern browsers. Which browser are you using that allowed alpha characters?

One thing i did notice is we can add emojis in the exchange 😲

image of a QSO entry with the laughing emoji entered in the exchange details

Copy link

Uh oh! @patrickrb, the image you shared is missing helpful alt text. Check #3023 (comment).

Alt text is an invisible description that helps screen readers describe images to blind or low-vision users. If you are using markdown to display images, add your alt text inside the brackets of the markdown image.

Learn more about alt text at Basic writing and formatting syntax: images on GitHub Docs.

@DJ3CE DJ3CE changed the title Prevent non-numeric input in contesting-serial-exchange fields Prevent non-numeric input in contesting-serial fields Mar 29, 2024
@DJ3CE
Copy link
Contributor Author

DJ3CE commented Mar 29, 2024

Thanks for your feedback. I have changed the title to avoid confusion, which fields are meant.

I could enter arbitrary data with Firefox into the 'numbers'-field, which got discarded without any mention.

Do I understand it right, that issues with control-keys weren't experienced but assumed by you? Again: I can not reproduce issues with control keys.

p.s. Uh yeah! I'd definitly go for a smiley in one of my next signal reports ;).

@magicbug
Copy link
Owner

magicbug commented Apr 2, 2024

I didnt give much thought for emjoiis being added!

@magicbug magicbug merged commit 55d24cd into magicbug:dev Apr 2, 2024
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants