• We just launched and are currently in beta. Join us as we build and grow the community.

Creating a Random Number Generator App using HTML, CSS, and Vanilla JS

mouadslider

Fun Creator
M Rep
0
0
0
Rep
0
M Vouches
0
0
0
Vouches
0
Posts
162
Likes
76
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
In this tutorial, you can learn to create a simple Random Number Generator web application. The tutorial aims to provide the students and beginners with a reference for learning some CSS tricks and Vanilla JS techniques. Here, I will be providing some scripts that demonstrate the creation of a Random Number Generator. The complete source code zip file is also provided.

How does the Random Number Generator work?

The Random Number Generator App is a simple web application that generates random numbers. It allows the users to set the minimum and maximum number of the number to be generated. If the set minimum number is greater than the maximum number, the will show a dialog that says that "Minimum Number must be greater than Maximum Number" and when the set numbers are equal, it will show "Minimum Number must not be equal to Maximum Number". The Generated Code will be shown above the form.

Creating the Random Number Generator App

Page Interface

Here is the HTML file script known as index.html. The file contains the page layout and other page elements.

  1. <!DOCTYPE html>
  2. <html

    lang

    =

    "en"

    >
  3. <head

    >
  4. <meta

    charset

    =

    "UTF-8"

    >
  5. <meta

    name

    =

    "viewport"

    content

    =

    "width=device-width, initial-scale=1.0"

    >
  6. <title

    >

    Vanilla JS Random Number Generator</

    title

    >
  7. <link

    rel

    =

    "stylesheet"

    href

    =

    "style.css"

    >
  8. <link

    rel

    =

    "preconnect"

    href

    =

    "https://fonts.googleapis.com"

    >
  9. <link

    rel

    =

    "preconnect"

    href

    =

    "https://fonts.gstatic.com"

    crossorigin>
  10. </

    head

    >
  11. <body

    >
  12. <div

    class

    =

    "container"

    >
  13. <h1

    id

    =

    "page-title"

    >

    Vanilla JS Random Number Generator</

    h1

    >
  14. <hr

    id

    =

    "title_hr"

    >
  15. <!-- Generator Wrapper -->
  16. <div

    id

    =

    "generator-wrapper"

    >
  17. <form

    action

    =

    ""

    id

    =

    "generator-form"

    >
  18. <div

    id

    =

    "generated-code"

    >

    0</

    div

    >
  19. <!-- Input Field -->
  20. <div

    class

    =

    "inline-form-field"

    >
  21. <!-- Minimum Number Field -->
  22. <div

    class

    =

    "form-field"

    >
  23. <label

    for

    =

    ""

    >

    Minimum Number</

    label

    >
  24. <input

    type

    =

    "number"

    name

    =

    "minimum"

    min

    =

    "0"

    required>
  25. </

    div

    >
  26. <!-- Minimum Number Field -->
  27. <!-- Maximum Number Field -->
  28. <div

    class

    =

    "form-field"

    >
  29. <label

    for

    =

    ""

    >

    Maximum Number</

    label

    >
  30. <input

    type

    =

    "number"

    name

    =

    "maximum"

    min

    =

    "0"

    required>
  31. </

    div

    >
  32. <!-- Maximum Number Field -->
  33. </

    div

    >
  34. <!-- Input Field -->
  35. <!-- Generate Code Button -->
  36. <div

    class

    =

    "btn-field"

    >
  37. <button

    id

    =

    "generate-btn"

    >

    Generate</

    button

    >
  38. </

    div

    >
  39. <!-- Generate Code Button -->
  40. </

    form

    >
  41. </

    div

    >
  42. <!-- Generator Wrapper -->
  43. </

    div

    >
  44. <script

    src

    =

    "script.js"

    ></

    script

    >
  45. </

    body

    >
  46. </

    html

    >

Stylesheet

Next, here's a CSS file script known as style.css. The file contains the stylesheet code of the page layout and form elements design.

  1. @import

    url

    (

    'https://fonts.googleapis.com/css2?f...00;1,200;1,300;1,400;1,500;1,600&display=swap" rel="stylesheet'

    )

    ;
  2. *

    {
  3. margin

    :

    0

    ;
  4. padding

    :

    0

    ;
  5. box-sizing

    :

    border-box

    ;
  6. font-family

    :

    'Dongle'

    ,

    sans-serif

    ;
  7. font-family

    :

    'Roboto Mono'

    ,

    monospace

    ;
  8. }
  9. ::

    selection

    {
  10. color

    :

    #fff

    ;
  11. background

    :

    #4db2ec

    ;
  12. }
  13. body{
  14. display

    :

    flex;
  15. align-items

    :

    center

    ;
  16. justify-content

    :

    center

    ;
  17. min-height

    :

    100vh

    ;
  18. background

    :

    #4facfe

    ;
  19. background-image

    :

    linear-gradient(

    to right

    ,

    #4facfe

    0%

    ,

    #00f2fe

    100%

    )

    ;
  20. padding

    :

    2em

    0

    ;
  21. }
  22. #page-title

    {
  23. color

    :

    #fff

    ;
  24. text-align

    :

    center

    ;
  25. font-weight

    :

    500

    ;
  26. text-shadow

    :

    0px

    0px

    15px

    #0000003a

    ;
  27. }
  28. #title_hr

    {
  29. width

    :

    60px

    ;
  30. border

    :

    2px

    solid

    #ffffff

    ;
  31. margin

    :

    .35em

    auto

    ;
  32. }
  33. @media

    (

    min-width

    :

    780px

    )

    {
  34. #page-title

    {
  35. width

    :

    780px

    ;
  36. }
  37. }

  38. div#generator-wrapper

    {
  39. width

    :

    480px

    ;
  40. margin

    :

    1em

    auto

    ;
  41. padding

    :

    2em

    1em

    ;
  42. background

    :

    #fff

    ;
  43. border

    :

    1px

    solid

    #d1d1d1

    ;
  44. box-shadow

    :

    0px

    0px

    10px

    #00000038

    ;
  45. }

  46. .inline-form-field

    {
  47. position

    :

    relative

    ;
  48. display

    :

    flex;
  49. width

    :

    100%

    ;
  50. flex-wrap

    :

    wrap;
  51. }

  52. .form-field{
  53. position

    :

    relative

    ;
  54. width

    :

    50%

    ;
  55. padding

    :

    15px

    20px

    ;
  56. }

  57. .form-field>

    label,
  58. .form-field>

    input{
  59. display

    :

    block

    ;
  60. width

    :

    100%

    ;
  61. }

  62. .form-field>

    label {
  63. color

    :

    #4e4e4e

    ;
  64. margin-bottom

    :

    5px

    ;
  65. }

  66. .form-field>

    input {
  67. padding

    :

    0.35em

    0.5em

    ;
  68. border

    :

    1px

    solid

    #c9bdbd

    ;
  69. outline

    :

    none

    ;
  70. }

  71. .form-field>

    input:

    focus

    {
  72. border-color

    :

    #94def596

    ;
  73. box-shadow

    :

    0px

    0px

    7px

    #94def547

    ;
  74. }

  75. .btn-field

    {
  76. display

    :

    flex;
  77. align-items

    :

    center

    ;
  78. justify-content

    :

    center

    ;
  79. }
  80. button

    #generate-btn

    {
  81. padding

    :

    0.35em

    0.75em

    ;
  82. background

    :

    #00adff

    ;
  83. color

    :

    #fff

    ;
  84. font-size

    :

    1.3rem

    ;
  85. font-weight

    :

    500

    ;
  86. border

    :

    none

    ;
  87. cursor

    :

    pointer

    ;
  88. width

    :

    100%

    ;
  89. border-radius

    :

    50px

    ;
  90. }

  91. button

    #generate-btn

    :

    hover

    {
  92. background

    :

    #0474a8

    ;
  93. box-shadow

    :

    0px

    0px

    10px

    #0474a844

    ;
  94. }

  95. div#generated-code

    {
  96. text-align

    :

    center

    ;
  97. font-size

    :

    4rem

    ;
  98. letter-spacing

    :

    2px

    ;
  99. }

JavaScript

Lastly, here's the JavaScript file script known as script.js. The file contains the code that generates the random number between the given minimum and maximum numbers upon submission.

  1. const

    generateEl =

    document.getElementById

    (

    'generated-code'

    )
  2. const

    form =

    document.getElementById

    (

    'generator-form'

    )

  3. form.addEventListener

    (

    'submit'

    ,

    function

    (

    e)

    {
  4. e.preventDefault

    (

    )
  5. var

    min =

    form.querySelector

    (

    'input[name="minimum"]'

    )

    .value
  6. var

    max =

    form.querySelector

    (

    'input[name="maximum"]'

    )

    .value
  7. min =

    parseInt(

    min)
  8. max =

    parseInt(

    max)
  9. if

    (

    min >

    max)

    {
  10. alert(

    "Maximum Number must be greater than Minimum Number"

    )

    ;
  11. return

    false

    ;
  12. }
  13. if

    (

    min ==

    max)

    {
  14. alert(

    "Minimum Number must not be equal to Maximum Number"

    )

    ;
  15. return

    false

    ;
  16. }
  17. var

    generatedCode =

    Math

    .floor

    (

    Math

    .random

    (

    )

    *

    (

    max -

    min +

    1

    )

    )

    +

    min;
  18. generateEl.innerText

    =

    generatedCode
  19. }

    )

Snapshots

Here are some snapshots of the web application script results.

Page Interface

Generator Wrapper

Generator Form

Generated Code

There you go! I have provided also the complete source code zip file on this website and it is free to download. The download button is located below this tutorial's content. Feel free to download and modify the source code the way you wanted.

That's it! I hope this Creating a Random Number Generator App using HTML, CSS, and Vanilla JS Tutorial will help you with what you are looking for and that you'll find something useful for your current and future web application projects.

Explore more on this website for more Tutorials and Free Source Codes.

Happy Coding =)


Download
You must upgrade your account or reply in the thread to view hidden text.
 

450,270

322,965

322,974

Top