User registration verification using js. JS doesn't show any errors, it just doesn't run


unusual phenomenon

Tried using simple JS to validate user password when registering, but the page doesn't show any errors and runs as if the JS doesn't even exist (although it's added at the end of the php file <script src="script.js"></script>). Any suggestions on what I'm doing wrong are welcome as I can't seem to figure it out. I'm completely new to JS but need to do this in school trying to implement it in the easiest way possible.

JS example:

const name = document.getElementById('name')
const password = document.getElementById('password')
const form = document.getElementById('registerForm')
const errorElement = document.getElementById('error')
const button = document.getElementById('signUp')

if (button) {
    form.addEventListener(button, (e) => {
        let messages = []
        if (name.value === '' || name.value == null) {
            messages.push('Name is required.')
        }
        if (password.value.length <= 6) {
            messages.push('Password must be longer than 6 characters.')
        }
        if (password.value.length >= 20) {
            messages.push('Password must be less than 20 characters.')
        }
        if (messages.length > 0) {
            e.preventDefault()
            errorElement.innerText = messages.join(', ')
        }
    })
}

Code snippet from html:

<form id="registerForm" class="register" method="post">
          <div id = "signup_form" class="textboxes">
          <input id="name" type = "text" name="name" placeholder="Name">
          <input id="email" type = "email" name="email" placeholder="Email">
          <input id="password" type = "password" name="password" placeholder="Password">
          <input id="repeat_password" type = "password" name="password_again" placeholder="Repeat password">
          <button id="signUp" type="submit" name="btn-register">Sign Up</button>
          <div id = "error"></div>
Sven writes code

You are just missing a few things. The HTML is malformed, to add an event listener you need to use the followingtarget.addEventListener(type, listener [, options]);

Here you want to have the "target" as the button because that will trigger the action. See here for more information .

const name = document.getElementById('name')
const password = document.getElementById('password')
const form = document.getElementById('registerForm')
const errorElement = document.getElementById('error')
const button = document.getElementById('signUp')

if (button) {
  button.addEventListener('click', (e) => {
    let messages = []
    if (name.value === '' || name.value == null) {
      messages.push('Name is required.')
    }
    if (password.value.length <= 6) {
      messages.push('Password must be longer than 6 characters.')
    }
    if (password.value.length >= 20) {
      messages.push('Password must be less than 20 characters.')
    }
    if (messages.length > 0) {
      e.preventDefault()
      errorElement.innerText = messages.join(', ')
    }
  })
}
<form id="registerForm" class="register" method="post">
  <div id="signup_form" class="textboxes">
    <input id="name" type="text" name="name" placeholder="Name" />
    <input id="email" type="email" name="email" placeholder="Email" />
    <input id="password" type="password" name="password" placeholder="Password" />
    <input id="repeat_password" type="password" name="password_again" placeholder="Repeat password" />
    <button id="signUp" type="submit" name="btn-register">Sign Up</button>
    <div id="error"></div>
  </div>
</form>

Related


Validate.js doesn't show errors

username So I am using Validate.js to validate the form through the front end. But for some strange reason the error is not showing up. I'm also not sure if it was validated from the start. Any ideas? Here is my code: <html> <head> <script type="text/javascrip

Validate.js doesn't show errors

username So I am using Validate.js to validate the form through the front end. But for some strange reason the error is not showing up. I'm also not sure if it was validated from the start. Any ideas? Here is my code: <html> <head> <script type="text/javascrip

My python code doesn't run and doesn't show any errors

Ulysses Barbera > print('¡Hi, welcome!') > while(True): > option = input('''\n¿What you want do? ¡Select an option! > (A) Greet > (B) Add two numbers > (C) Exit''') > if option == 'A': > print('\n The developer sends his regards \n')

D3js tutorial doesn't show any graphics and no errors in console

Flo I have the following HTML page based on this tutorial : https://bl.ocks.org/mbostock/7322386 . However, the line chart is not displayed and there are no errors in the console. When I uncomment that section, I get the error:d3.scale.linear Uncaught TypeErro

Python asyncio doesn't show any errors

superman I am trying to get some data from thousands of URLs by using asyncio. Here is a brief overview of the design: QueuePopulate a bunch of URLs at once with one URLProducer generate a bunchConsumers Each Consumerkeeps fetching urls from Queueand sending G

Python asyncio doesn't show any errors

superman I am trying to get some data from thousands of URLs by using asyncio. Here is a brief overview of the design: QueuePopulate a bunch of URLs at once with one URLProducer generate a bunchConsumers Each Consumerkeeps fetching urls from Queueand sending G

PHP doesn't show any errors

UserIsCorrupt I know this question has been asked many times in the past, but none of the solutions worked for me. Here is my PHP code: <?php function ?> This should generate an error. However, it just returns a 500 error and doesn't load. I tried the less

Python asyncio doesn't show any errors

superman I am trying to get some data from thousands of URLs by using asyncio. Here is a brief overview of the design: QueuePopulate a bunch of URLs at once with one URLProducer generate a bunchConsumers Each Consumerkeeps fetching urls from Queueand sending G

Python asyncio doesn't show any errors

superman I am trying to get some data from thousands of URLs by using asyncio. Here is a brief overview of the design: QueuePopulate a bunch of URLs at once with one URLProducer generate a bunchConsumers Each Consumerkeeps fetching urls from Queueand sending G

Electron window doesn't open but doesn't show any errors

Jeff Context: I 've seen this question, but it doesn't solve my problem. When I try to run my electron app, I don't get any errors and it seems to compile, but the electron window doesn't pop up. Reply: Date: 2019-05-08T03:02:22.036Z Hash: 3303fd48d099a538493f

ionic cordova doesn't build but doesn't show any errors

Tristan I just made a "new" ionic app, but I can't get Cordova to build the APK. I put the new quotes because I created a new project and copied over from the previous project's src. Something went horribly wrong with the dependencies of the plugin installed o

Ajax doesn't work. it doesn't show any errors

Ati I am using Ajax in my application. Previously it worked fine and checked many times. Not working now. I didn't change anything in this code. I checked the URL path and also checked the Jquery click event. It doesn't show any errors in the console log. $(".

Electron window doesn't open but doesn't show any errors

Jeff Context: I 've seen this question, but it doesn't solve my problem. When I try to run my electron app, I don't get any errors and it seems to compile, but the electron window doesn't pop up. Reply: Date: 2019-05-08T03:02:22.036Z Hash: 3303fd48d099a538493f

Electron window doesn't open but doesn't show any errors

Jeff Context: I 've seen this question, but it doesn't solve my problem. When I try to run my electron app, I don't get any errors and it seems to compile, but the electron window doesn't pop up. Reply: Date: 2019-05-08T03:02:22.036Z Hash: 3303fd48d099a538493f

ionic cordova doesn't build but doesn't show any errors

Tristan I just made a "new" ionic app, but I can't get Cordova to build the APK. I put the new quotes because I created a new project and copied over from the previous project's src. Something went horribly wrong with the dependencies of the plugin installed o

(Chart.js) webpage doesn't show chart, no errors in console

lucky I'm new to chart.js and trying to build something simple. I get no errors in the console, but the graphs are not showing up on my webpage. I don't know where it came from. It would be really nice if someone could help. <!DOCTYPE> <html> <head> <script

Node.js doesn't always show errors

User 3923317 I started learning js and nodejs. I ran into a problem and it took me a long time to solve. I'm using the bcrypt library and made a typo on the require statemant: const bcrpyt = require('bcryptjs'); Then in my pre-save modal, I used: bcrypt.hash(u

(Chart.js) webpage doesn't show chart, no errors in console

lucky I'm new to chart.js and trying to build something simple. I get no errors in the console, but the graphs are not showing up on my webpage. I don't know where it came from. It would be really nice if someone could help. <!DOCTYPE> <html> <head> <script

(Chart.js) webpage doesn't show chart, no errors in console

lucky I'm new to chart.js and trying to build something simple. I get no errors in the console, but the graphs are not showing up on my webpage. I don't know where it came from. It would be really nice if someone could help. <!DOCTYPE> <html> <head> <script

socialfeed.js doesn't show anything, but also no errors

Satch3000 I'm trying to get a basic feed to display content using: socialfeed.js but nothing is showing and im not getting any errors. The complete code is as follows: <!doctype html> <html> <head> <meta charset="utf-8" /> <!-- Social-feed css -->

Ember.js sometimes doesn't show errors

chopped Sometimes the ember app crashes, usually when I'm doing something at an earlier stage, but I haven't really seen a pattern - and no errors are logged to the console. If I set a breakpoint somewhere TRY_CATCH_ERROR.error = e;in the rendered app guts I s