Javascript script is not running

Asked

Viewed 39 times

1

I have the following problem...

I have this code in JS:

window.addEventListener('DOMContentLoaded', function(){

    const regexCharactere = /<li>.[^<* ]+/;
    const regexEmail = /[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/;;
    const nome = document.getElementsByClassName('nome');
    const email = document.getElementsByClassName('email');
    const buttonForm = document.getElementsByClassName('buttonForm');
    buttonForm.addEventListener('click', (e) => {
        alert("testando");
        if(!regexEmail.test(email.value)  {

            alert('Formato do e-mail inválido');
            email.focus();
            e.preventDefault();

        }
        if(!regexCharactere.test(nome.value) {

            alert('Nome inválido');
            nome.focus();
            e.preventDefault();

        } 

    });
});

Structure HTML

<form method="POST" action="#">

  <label class="left">Nome</label>
  <input type="text" class="form-control" name="nome" id="nome" placeholder="Nome">

  <label class="left">E-mail</label>
  <input type="email" class="form-control" name="email" id="email" placeholder="E-mail">

  <label class="left">Telefone/Celular</label>
  <input type="tel" class="form-control" name="telefone" id="telfone" placeholder="Telefone/Celular">

  <button type="submit" class="waves-effect waves-light cta btn-large btn-solicitar-modal pulse-hover buttonForm"> Solicitar curso </button>
  </form>

When I try to inject it into my page no error appears from script, but does not perform as I expected.

I’ve already done some tests:

  • I added the window initially to load the script
  • I changed some fields of my HTML and JS, but still no success
  • I tried to apply a alert (as shown in the script) to see if it captures, but without success as well.
  • I changed in the buttonForm.addEventListener('click', (e) => { /// } for buttonForm.addEventListener('submit', (e) => { /// } but it didn’t work either

What’s wrong with mine script?

Edit: In case I haven’t been clear enough, I’ll make the changes.

  • Would not be document.getElementsByClassName('buttonForm')[0] ?

  • I do not know very well what it is to pick up in the position [0]... But in addition to this form, there are 3 other identical forms on the pages (but in different resolutions)... Well, can you explain to me what [0]?

  • It would be interesting to place an event of submit in the form instead of the button.

  • So, I changed the button for a form variable... But also nothing is happening: It was like this: const form = document.forms['marketing'].value; and then form.addEventListener('submit', (e) => {});.

  • What you have most is an error in your script, such as closing ( of if's.

  • I don’t quite understand @Sam, can you be clearer? Because I’m establishing a condition there... Thanks for noting the error in the script, but I would like to understand better.

Show 1 more comment

1 answer

2


There are many errors in your script. You’re probably not seeing them on the console because the form is being submitted quickly in time for you not to see the errors shown on the console, as the page undergoes a quick refresh to the # in the action of form.

Mistakes:

The fields "name" and "email" do not have class, only name and id, soon the document.getElementsByClassName becomes invalid. You could take the value of these fields or with document.getElementsByName('name_do_campo')[0] or document.getElementById('id_do_campo').

In the case of document.getElementsByName the index is required [0] because this method returns a nodelist (an array). Already the document.getElementById no index needed because it only takes a single element (see that the method name is in the singular).

The button submit has a class and can be captured with document.getElementsByClassName('buttonForm')[0].

Another error is the regex where the bounding bars have to escape / in the middle of expression:

/[a-z0-9!#$%&'*+\/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+\/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/;
                ↑                                 ↑

Another mistake is that the parentheses of if's:

if(!regexEmail.test(email.value)) {
                                ↑

and

if(!regexCharactere.test(nome.value)) {
                                    ↑
  • Oops! Thanks for the corrections, I’ll try to see if I can now. Those little mistakes I didn’t notice, it was really something absurd rs.

  • 1

    Quiet friend. Q doubt is just talk.

  • is not a doubt but a advice... Do you have any booklets, course, or content knowledge about programming? Where to start, I often end up getting lost in basic concepts, others I do not know in depth (such as data structure, trees, positions, etc)... If you have could share the knowledge with us (community)? For us (lay and new in the programming part) is a great help. Thanks in advance.

  • Dude, I don’t know any courses or booklets, all I learned was trying to do and many things I learned here on the site same. A good site to start is W3schools. There is a lot of usable stuff for beginners.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.