Skip to content
  • Unlock Pro
  • Log in with GitHub
Solution
Submitted almost 3 years ago

Tip Calculator App - Live formatting / validation, CUBE CSS

cube-css
Adam Wozniak•230
@adamwozhere
A solution to the Tip calculator app challenge
View live sitePreview (opens in new tab)View codeCode (opens in new tab)

Solution retrospective


This project was surprisingly difficult because of the radio group and custom input formatting. I'm not sure if there is a best practice for implementing this type of radio group; where a text input is effectively a radio button also. Perhaps there is a better way of handling this but I used an extra hidden radio button with the value custom -- then in the JS file, if the radio group value is custom then it will calculate the bill using the value of the custom tip input instead.

I also decided to live-format / validate the inputs which complicated things. I found it quite difficult and so my JS file became like spaghetti quite quickly. I think a better approach would be to use Web Components or a JS framework to compartmentalise the input components better and handle state. However it does function, and I learnt a lot by attempting this.

For the custom input formatting, I implemented the following:

Currency (Bill) Input:

  • allows only numbers to be input, always starting from the first digit
  • cursor is always set to the end (the user cannot select and input numbers in the middle of a value)
  • automatically formats as a currency on keydown (including decimal symbols etc.)

This forces the mode of input, stopping the user from entering strange numbers such as multiple leading zeros or decimal symbols / separators in strange places such as: 00000.123,456.0.0.00.

e.g. To input the formatted number 123,000.50, the user inputs 12300050 using only the number keys (backspace can be used to edit the number if needed.) However the user can input the decimal symbols / separators , . as they will be ignored, but still formatted on keydown.

Integer (Number of Persons) Input: The same as the currency input, except there are no decimals (forcing integers only).

Percentage (Custom tip) Input: The same as the integer input, except:

  • automatic formatting of the % symbol (focus sets cursor to just before the symbol instead of after)
  • number limited to 0 - 100
  • if the input is selected but left blank, it will format to 0% on losing focus
  • if the input is set to 0% then re-selected, it is formatted back to % so the user does not have to delete the leading 0 before entering a new value
Code
Loading...

Please log in to post a comment

Log in with GitHub

Community feedback

No feedback yet. Be the first to give feedback on Adam Wozniak's solution.

Join our Discord community

Join thousands of Frontend Mentor community members taking the challenges, sharing resources, helping each other, and chatting about all things front-end!

Join our Discord
Frontend Mentor logo

Stay up to datewith new challenges, featured solutions, selected articles, and our latest news

Frontend Mentor

  • Unlock Pro
  • Contact us
  • FAQs
  • Become a partner

Explore

  • Learning paths
  • Challenges
  • Solutions
  • Articles

Community

  • Discord
  • Guidelines

For companies

  • Hire developers
  • Train developers
© Frontend Mentor 2019 - 2025
  • Terms
  • Cookie Policy
  • Privacy Policy
  • License

Oops! 😬

You need to be logged in before you can do that.

Log in with GitHub

Oops! 😬

You need to be logged in before you can do that.

Log in with GitHub

How does the accessibility report work?

When a solution is submitted, we use axe-core to run an automated audit of your code.

This picks out common accessibility issues like not using semantic HTML and not having proper heading hierarchies, among others.

This automated audit is fairly surface level, so we encourage to you review the project and code in more detail with accessibility best practices in mind.

How does the CSS report work?

When a solution is submitted, we use stylelint to run an automated check on the CSS code.

We've added some of our own linting rules based on recommended best practices. These rules are prefixed with frontend-mentor/ which you'll see at the top of each issue in the report.

The report will audit 1st-party linked stylesheets, and styles within <style> tags.

How does the HTML validation report work?

When a solution is submitted, we use html-validate to run an automated check on the HTML code.

The report picks out common HTML issues such as not using headings within section elements and incorrect nesting of elements, among others.

Note that the report can pick up “invalid” attributes, which some frameworks automatically add to the HTML. These attributes are crucial for how the frameworks function, although they’re technically not valid HTML. As such, some projects can show up with many HTML validation errors, which are benign and are a necessary part of the framework.