vendredi 3 mai 2019

Same regex wont work on different input fields?

I am having a problem with regex and some if statements . I have classic form with "name" , "lastName" and "email".

I am using simple validation with these if statements

function addTask(e) {

    const filter = /^([a-zA-Z0-9_\-\.]+)@([a-zA-Z0-9_\-\.]+)\.([a-zA-Z]{2,5})$/;
    const filterName = /[a-zA-Z]/
    

    if (!filter.test(email2.value)) {
        alert('please enter valid mail')


    }
    if (!filterName.test(lastName.value)) {

        showError('Please check your last name');
    }

    if (!filterName.test(yourName.value)) {
        showError('Please check your name');
     

    } else { 

        save(yourName.value, lastName.value, email2.value, text.value);

        const div = document.createElement('div');
        div.className = "testimonial";

        const div2 = document.createElement('div');
        div2.className = "pic";

        div.appendChild(div2);

        const img = document.createElement('img');
        img.src = "tormund.png";

        div2.appendChild(img);

        const p = document.createElement('p');
        p.className = "description";
        p.appendChild(document.createTextNode(text.value));

        div.appendChild(p);

        const h3 = document.createElement('h3');

        h3.className = "title";
        h3.appendChild(document.createTextNode(yourName.value));
        div.appendChild(h3);
        
        cards.appendChild(div);

    }


    e.preventDefault();
}
  
<input id="name" type="text" name="field1" placeholder="Name*" >
                                <input id="lastName" type="text" name="field2" placeholder="Last name*">

                                <input id="emailRev" type="email" name="field3" placeholder="Email *">

name and email if statements works perfectly ,they wont go straight to "else statement" and they stop running the function right away, but when it comes to last name, code continues directly to "else" and execute my card creation.

Also to mention, regex should be only text for both input fields. Does anyone knows where i get this wrong?

Aucun commentaire:

Enregistrer un commentaire