Number-only inputs aren't so straight-forward

Поділитися
Вставка
  • Опубліковано 23 вер 2024
  • Code: codepen.io/kev...
    #css
    --
    Come hang out with other dev's in my Discord Community
    💬 / discord
    Keep up to date with everything I'm up to
    ✉ www.kevinpowel...
    Come hang out with me live every Monday on Twitch!
    📺 / kevinpowellcss
    ---
    Help support my channel
    👨‍🎓 Get a course: www.kevinpowel...
    👕 Buy a shirt: teespring.com/...
    💖 Support me on Patreon: / kevinpowell
    ---
    My editor: VS Code - code.visualstu...
    ---
    I'm on some other places on the internet too!
    If you'd like a behind the scenes and previews of what's coming up on my UA-cam channel, make sure to follow me on Instagram and Twitter.
    Twitter: / kevinjpowell
    Codepen: codepen.io/kev...
    Github: github.com/kev...
    ---
    And whatever you do, don't forget to keep on making your corner of the internet just a little bit more awesome!

КОМЕНТАРІ • 273

  • @KevinPowell
    @KevinPowell  Рік тому +79

    Code: codepen.io/kevinpowell/pen/dyjwWEY

  • @veni_vidi_victorian
    @veni_vidi_victorian Рік тому +2659

    Double it and pass it to the next back-end developer

    • @kristian9268
      @kristian9268 Рік тому +121

      Hey Backend Dev? Can I get an Angel Shot

    • @AnwerDulia
      @AnwerDulia Рік тому +102

      Double it and pass it to the sys admin

    • @coderswat
      @coderswat Рік тому +5

      ​@@kristian9268 sure

    • @la9955nis
      @la9955nis Рік тому +5

      ​@@kristian9268 who

    • @Maniac-007
      @Maniac-007 Рік тому +1

      Would you rather have a gay son or a thot daughter?

  • @MysterX79
    @MysterX79 Рік тому +425

    and never forget "e" is a valid numbersymbol.
    I had a wonderfull bug a half year ago with a sudden apperance of a HUGE exponent number. 😅

    • @evanjoyal9540
      @evanjoyal9540 Рік тому +36

      omg. so thats why my number field was bugged. sooo annoying

    • @ChibiBlasphem
      @ChibiBlasphem 9 місяців тому +10

      Oh nice to know, but that's also why we should have validation on input so it can't go higher than a max

    • @MaximNightFury
      @MaximNightFury 9 місяців тому +3

      I gotta test this with some code I whipped up in Python... If it treats e like a number symbol

    • @MysterX79
      @MysterX79 9 місяців тому +3

      ​@@MaximNightFury Back in August'21 I wrote a note to my coworkers about this issue.
      My suggested solution looked like that ...

    • @Impact_TO
      @Impact_TO Місяць тому

      Oh hell nah that Sounds horrible 😂😂

  • @MarcelRobitaille
    @MarcelRobitaille Рік тому +1148

    Credit card numbers are not numbers, just like phone numbers are not numbers. It only makes sense to treat it as a number if it makes sense to take half of it. If you ask for half of someone's credit card number, they will probably give you the first 8 digits, not divide it by 2. It's a string.

    • @poseidonsmafia1160
      @poseidonsmafia1160 Рік тому +192

      That is such a creative way to think about it. Making a difference between a number and a sequence of single numbers.

    • @capsey_
      @capsey_ Рік тому +76

      ​@@poseidonsmafia1160 there is a word for a single number - digit!

    • @poseidonsmafia1160
      @poseidonsmafia1160 Рік тому +18

      @@capsey_ at the top of my tongue xD thank you!

    • @snailedlt
      @snailedlt Рік тому +5

      So it's an array of numbers?

    • @capsey_
      @capsey_ Рік тому +28

      @@snailedlt array of digits (because one number can consist of many digits and even symbols), or more accurately, sequence of digits

  • @joshuafountain
    @joshuafountain Рік тому +152

    Waiting for an npm package with 7 dependencies and 4 files to fix numeric inputs

    • @mthalter
      @mthalter 9 місяців тому +3

      Would

    • @DEBO5
      @DEBO5 9 місяців тому +2

      You’re more than welcome to spin something up from scratch but who has time for that?

  • @bramus
    @bramus Рік тому +238

    In Chrome, also accepts the scientific notation, e.g. `1e7` … technically correct, but yet another reason to stay away from it.

    • @Zooiest
      @Zooiest Рік тому +8

      Probably uses the built-in Number conversion, and if so, practically any JS numeric literal would be considered valid (notably prefixed literals, special float numbers like NaN and Infinity, and so on)

  • @ravel1274
    @ravel1274 6 місяців тому +9

    In short :
    " fed up with js? We've got your back ! Here's some css"
    " fed up with css? We've got your back ! Here's some js"

  • @MrBleb
    @MrBleb 10 місяців тому +1

    I’ve been taking a boot camp for the past month and the information you give out makes me SO excited to learn more about web development. Thank you for all these shorts, and all the awesome videos you have!

  • @ThomasSselate
    @ThomasSselate Рік тому +75

    Web development is a nightmare

    • @oliveryt7168
      @oliveryt7168 10 місяців тому +4

      What is not?

    • @trenwar
      @trenwar 10 місяців тому +17

      Sounds like skill issue

    • @Joe-SoftwareEngineer
      @Joe-SoftwareEngineer 10 місяців тому +6

      Web development is fun

    • @pedrovsn
      @pedrovsn 10 місяців тому

      No it is not. You just gotta study and practice a lot for mastery

    • @trenwar
      @trenwar 10 місяців тому

      @@TollyH agree, i actually don't really like web programming (only the front end tho) but that's where most of the money is, also i do back end so i guess it's less confusing than front end

  • @stillready6405
    @stillready6405 11 місяців тому +2

    I was just searching for that short, because now I'm working on a form and I could not quite remember all of that. Pretty neat :)

  • @rahulr9539
    @rahulr9539 9 місяців тому +4

    Remember to allow minus and dot if you need it in the number

  • @Koroistro
    @Koroistro 8 місяців тому +1

    [0-9]+ as a regex also prevents writing dots or commas which would prevent inputting decimals, so be careful when using that kind of filters.
    Ideally you want to come up with consistent standard rules for your fields and reuse them.
    Numbers can come in many flavours, integers, floats, percent values, scientific notation etc

  • @BrunoNeureiter
    @BrunoNeureiter 8 місяців тому +1

    A short with a CodePen. Insanity. THANKS!

  • @DTUSEM
    @DTUSEM Рік тому +19

    Read UK gov web design specifications. Very good specification

  • @mwmm
    @mwmm Рік тому +15

    The inconsistencies with input between browsers and platforms really bugs me, never quite understood why datetime never really works but datetime-local does on some browsers even though it is a newer standard

  • @Linuxdirk
    @Linuxdirk Рік тому +14

    Don’t forget that no data entered in the front-end should be given to the back-end without proper validation (input type hints and clientside checks are not proper validation).

    • @KevinPowell
      @KevinPowell  Рік тому +9

      100%! This is just for a better UI, nothing here about validation 🙂

    • @Linuxdirk
      @Linuxdirk Рік тому +5

      @@KevinPowell "Front-end is a warzone! A good looking one, though ... but trust no-one, especially not your users" 😆

    • @danielegvi
      @danielegvi Рік тому +1

      You’re saying front-end data cannot given to the back-end without validation, but don’t you need to give it to the back-end do the validation in the first place?

    • @Linuxdirk
      @Linuxdirk Рік тому

      @@danielegvi User input validation for me isn't part of the back-end. The back-end is the database, the storage, the business logic, etc.

    • @danielegvi
      @danielegvi Рік тому

      @@Linuxdirk If validation isn’t part of the back-end, and it isn’t part of the front-end, then where is it?

  • @kc-ep8yx
    @kc-ep8yx Рік тому +6

    Your content is so helpful Kev, keep going & thank you

  • @mtime6648
    @mtime6648 2 місяці тому

    Saving numbers as Text/Varchar in dbase is very good I say. For example when storing pin numbers for debit cards they may give numbers starting with zero, if we have used numbers the initial zeroes would have neglected. So it is advisable to go with text in such cases.

  • @theamazingsolt
    @theamazingsolt Рік тому +5

    It also adds a wonky scroll handler. Good tip

  • @fadge316
    @fadge316 Рік тому

    Omg Kevin I love you! Literally been thinking about this problem for the past week

  • @silentshadow867
    @silentshadow867 Рік тому

    I always want to save these videos, but realize if I did I would probably save every single Kevin Powell video I came across.

  • @starlines07
    @starlines07 17 днів тому

    You know you are a life saver for lot of devs 😂

  • @XDBjoernXD
    @XDBjoernXD Рік тому

    Since I know your channel I feel more motivated to learn CSS. You help me alot. Thank you and have a nice day.

  • @Innengelaender
    @Innengelaender Рік тому

    I've literally just had this issue in react and it drove me crazy because i didn't know it wouldnt fire the onchange event if it wasn't a numeric value but still display the changed value

  • @kenanhamzic2346
    @kenanhamzic2346 Рік тому +1

    Thank you for all your tips videos

  • @sexntuna
    @sexntuna Рік тому +22

    I always just use type=tel. Shows number keyboard only without the up/down arrows.

    • @seanthesheep
      @seanthesheep 10 місяців тому +4

      Hmmm, I feel like this could be semantically confusing because it could suggest phone numbers when it shouldn't. In your experience, does it work well with browser auto fill and screen readers?

    • @sexntuna
      @sexntuna 9 місяців тому

      @@seanthesheep doesn’t autocomplete”off” takes care of that?

    • @seanthesheep
      @seanthesheep 9 місяців тому

      @@sexntuna mobile keyboards might still suggest phone numbers in a non-intrusive chip

    • @MinorTom
      @MinorTom 9 місяців тому

      ​@@sexntunaautocomplete="off" is ignored by some browsers.

    • @fullfungo
      @fullfungo 8 місяців тому

      Do you not care about accessibility?

  • @TheScriptPunk
    @TheScriptPunk 8 місяців тому

    Use 2 input fields. In key press, dispatch the same event to the number only input, and take the value, set the value of the main input field

  • @lxbdr
    @lxbdr Рік тому +2

    THANK YOU! haven't tried your solution yet, but I hate type="number" fields as with an Apple Magic Mouse I tend to accidentally scroll over them changing the value...

  • @nsshurtz
    @nsshurtz 10 місяців тому +1

    Android definitely depends on which keyboard is configured. iOS probably as well, but they're more restricted on what keyboards can actually do. Also, on Android, keyboards can (but are highly encouraged not to) completely ignoring the IME hints.

  • @JMIK1991
    @JMIK1991 Рік тому

    Yes, did something like this few years ago and Safari was a pain.

  • @AndyClements
    @AndyClements Рік тому

    Better still be checking that on the back end. Just because the pretty page limits it doesn't mean I can't send other data.

  • @justingolden87
    @justingolden87 Рік тому

    You can type dash "-" period "." and "e" in number inputs. Which CAN all be valid numeric values, but can also be gibberish such as "4--.4e..e44.."

  • @Tru_lie13
    @Tru_lie13 26 днів тому

    I didn't know about 'pattern' and have been using regex.

  • @ChikaRose-qj5nm
    @ChikaRose-qj5nm Рік тому

    I love ur style for css creation, and love to follow your thoughts. Great thanks. - GMB Uneke Martin S.

  • @voidbinary
    @voidbinary Рік тому +1

    Wouldn't it be smarter to get rid of the nested EventListener of input and use the preventDefault method to prevent input from being entered?
    The else then becoming beforeValue = el.value

  • @kilianlindberg
    @kilianlindberg 27 днів тому

    .. just as much JavaScript Validation in any case and most importantly perhaps (if theres a backend; perhaps nodejs though), server-side cleaning too (which I normally I min my frameworks automate to be one and the same before a SQLite meshes it into a ball of cymatics iced water storage for my reflective light driven quantum computers etc) 🤓

  • @Lampe2020
    @Lampe2020 8 місяців тому +1

    Well, when I have an input with type=text and pattern= I can enter whatever characters I want but on submitting the form it focuses the incorrectly-used field and tells me to "Please use the specified format". I optimize my own website purely for Firefox (I have one Chrome-specific fix in there, but everything else is basically unsupported on non-Firefox browsers).

  • @manojht8036
    @manojht8036 10 місяців тому

    You can replace value in place to take only number ... and replace text with empty string .

  • @DEBO5
    @DEBO5 9 місяців тому

    Anytime you have even a simple contact form that needs to be shipped to production there will need to be quite a few different security features like csrf token, client side and server side validation and sanitization, probably a google captcha and a few other things like Content Security Policy and anti click jacking scripts, etc.

  • @jixster1566
    @jixster1566 9 місяців тому +1

    How has this not been abstracted

  • @giviko1709
    @giviko1709 Рік тому

    I actually needed this

  • @prhasn
    @prhasn Рік тому

    Excellent tip. Thanks

  • @paul_wiggin
    @paul_wiggin Рік тому

    decades of web-development, new browsers.. and here we are - discussing how to make a simple numeric input

  • @immortalsun
    @immortalsun 10 місяців тому

    This is pretty useful! Thanks!

  • @ImHansana123
    @ImHansana123 7 місяців тому

    You probably need to put this into stack overflow.

  • @yashmmee
    @yashmmee Рік тому +1

    I learned all this in hard way few days ago 🥴

  • @alexnezhynsky9707
    @alexnezhynsky9707 Рік тому

    Thanks!

  • @SXsoft99
    @SXsoft99 10 місяців тому

    Menwhile me doing some backend form validation and that's a wrap, hiding the number controls with css isnt that hard since tou copy paste it from stack overflow
    Bigest problem is when you have to deal with digits and some countirs use "." While others use "," and viceversa for thousand separator because accountants want to reinvent math

  • @mechafiki
    @mechafiki 8 місяців тому

    I think I'll go with the Css method

  • @fafutuka
    @fafutuka 6 місяців тому

    Things get out of control really quickly😂

  • @amithbhagat
    @amithbhagat Рік тому

    Thanks 🙏 learnt something new.

  • @pepito7666
    @pepito7666 11 місяців тому

    Bro.
    Videos like yours, Will be unnecesary, due yo ChatGPT.
    Thank God by your gift to humanity, ChatGPT.

  •  9 місяців тому

    note: do not forget to validate that also on backend

  • @Gascha102
    @Gascha102 10 місяців тому

    thank you so much

  • @petraveryanov2572
    @petraveryanov2572 9 місяців тому

    You can enter 'e' in number input in any browser (For scientific notation). Guess everyone who ever faced number input should have known it.

  • @thequickcode5144
    @thequickcode5144 9 місяців тому

    super useful thanks

  • @MasayaShida
    @MasayaShida Рік тому +1

    Please make one about handling currency inputs!

    • @KevinPowell
      @KevinPowell  Рік тому +1

      Not sure I could do that one in a short 😅

    • @MasayaShida
      @MasayaShida Рік тому

      @@KevinPowell ahh you're right. Pls consider as a full video idea!

  • @trplnr
    @trplnr 8 місяців тому

    dont forget that people that have knowledge could just, **inspect element**, validate with js on client, an validate again on back end :)

  • @dashawk
    @dashawk 2 місяці тому

    how about automatically populating the input from the received sms like an OTP?

  • @bobbymannino
    @bobbymannino Рік тому +1

    My mind is litterly blown

  • @webdbase
    @webdbase 8 місяців тому

    Don't rely on browsers' compatibility, html5, or whatever. Treat it as if there is no browser. The back-end should double check everything.

  • @temp50
    @temp50 8 місяців тому

    It is actually very easy:

  • @m4rt_
    @m4rt_ Рік тому

    also you need input validation on the server side.

  • @blal
    @blal Рік тому +1

    thanks

  • @ochaun
    @ochaun Рік тому

    Just use a whole bunch of JavaScript. If you hide the scroll arrows it doesn't make them go away. Sometimes scrolling on the mouse wheel forces and increment or decrement

  • @ieatsoap6389
    @ieatsoap6389 9 місяців тому +1

    YEAH!!!!!!!

  • @IlPandax
    @IlPandax Рік тому

    You could use Inputmask to accept only numbers cross browser.

  • @alexus8610
    @alexus8610 Рік тому +1

    How does this affect the experience in terms of accessibility?

    • @seanthesheep
      @seanthesheep 10 місяців тому

      This is a valid concern. However, gov.uk promotes it and they are big on accessibility, so I assume they've taken it into consideration.
      I assume that screen readers don't really announce what the limited set of characters are based on the HTML, and instead leave it up to the input's label

  • @mvargasmoran
    @mvargasmoran 9 місяців тому

    I reject every property when they don't work, in my head pattern doesn't work.

  • @MB-zj3er
    @MB-zj3er Рік тому

    Funny, I just did this the other day. 👍

  • @davejackson88
    @davejackson88 11 місяців тому

    nice work so Interesting

  • @tamas5931
    @tamas5931 8 місяців тому

    understatement 😅

  • @user-fguhiasvhjvbajkfjkbnas

    I'm a Japanes.
    I translated it, so the English may be wrong.
    I have specified this on my computer and it doesn't go away.

  • @code.cracking
    @code.cracking Рік тому

    Great man

  • @wesker0143
    @wesker0143 Рік тому

    What about use regular expression to validate the input value? I think it's more easy 🤔

  • @zvagolskyaleksey168
    @zvagolskyaleksey168 Рік тому

    How about to use min/max value?

  • @aminebouobida877
    @aminebouobida877 Рік тому

    Nice shorts, i really hate the default "select-option" style, please can you show me how to make it better

  • @christopherjung2697
    @christopherjung2697 Рік тому

    Thx!

  • @suvenduchatterjee114
    @suvenduchatterjee114 9 місяців тому

    Simply used input[type=tel]

  • @CC-1.
    @CC-1. Рік тому +1

    Js isn't complex for that just read input use delay loops 1 mili for and case statement for each else = "" simple

  • @victorgarcia3526
    @victorgarcia3526 11 місяців тому

    And how does it affect to usability of disabled people?

  • @toyin5522
    @toyin5522 Рік тому

    I think there is a type="numeric"

  • @JasonCarzano-i3n
    @JasonCarzano-i3n 10 місяців тому

    inputMode not working on my react

  • @gaurabgogoi2848
    @gaurabgogoi2848 11 місяців тому

    The js is not efficient, I am using it in single line,
    returning the vale only number in ASCII range.

  • @sunofabeach9424
    @sunofabeach9424 10 місяців тому

    don't you have like patterns for text windows?.. I saw this thing even in a Lua UI library...

  • @cruzinsweetsntreats
    @cruzinsweetsntreats Рік тому

    As I am setting up an auto calculated rate form, I ended up setting the final "else" statement with a formatted Intl.NumberFormat of "style: currency, currency: USD" for $0.00 for that error handling for accidental typing of [A-Za-z].
    I am offering a dedicated delivery service, and needed to have it calculate at different rates between mileage ranges. And it works. :D 06-12-23 0037 PT

  • @Alacrity69
    @Alacrity69 9 місяців тому

    Who else thought of fireship when hearing the voice?

  • @scyfox.
    @scyfox. Рік тому

    Try to input floating numbers and everything goes to hell because decimals with comma aren't working at default

  • @girishbanua
    @girishbanua Рік тому

    I was just thinking for a solution for this 😮and the video popped up... literally our minds are being hacked guys!!!!

  • @mehdib114
    @mehdib114 Рік тому

    you also have input="tel" to have number only

  • @MatheusHP
    @MatheusHP Рік тому

    amazing!!

  • @namenamington
    @namenamington 10 місяців тому

    These aren't normal numbers, these are *numeric* numbers.

  • @jfftck
    @jfftck Рік тому

    I love that Apple once again has to have a special way of doing things, they couldn’t just use the number pad without an extra property. All the Apple only standards are just a continuation of how all computers used to be with each creating their own system and requiring developers to conform to each “standard.” This is why Safari is the new IE.

  • @DioArsya
    @DioArsya Рік тому

    yup such a hassle

  • @MuffinMufster
    @MuffinMufster 9 місяців тому

    Type="tel" allows numeric input and shows numeric keypad on mobile.
    You are welcome

  • @oliveryt7168
    @oliveryt7168 10 місяців тому

    How did you know that I.... love CSS!?

  • @justizirtum
    @justizirtum 9 місяців тому

    I hate it when i only have numeric Keyboard in phon,cause i cant Copy Paste anymore

  • @JointDoktor
    @JointDoktor Рік тому

    And what about floating point numbers? 🤔

  • @aditya.khapre
    @aditya.khapre Рік тому

    They could just add another attribute to hide the arrows, but nooooooo, they like to see us suffer

  • @memesworld8870
    @memesworld8870 Рік тому

    Whats the difference between beforeinput and keypress/keydown/keyup? I never got the actual difference between these!!

    • @KevinPowell
      @KevinPowell  Рік тому +1

      It's for inputs... After the user has pressed something to add to the input, but before that input is added to the actual element, so it's perfect for this type of use case, where you need to modify what they're trying to put in by limiting the acceptable characters.