1

this is my html

<input type="text" id="test">
<button id="btn" > validate </button>
<span id="result"> </span>

and this is my javascript

var test = document.getElementById("test");
var btn = document.getElementById("btn");
var result = document.getElementById("result");

var regex = /^\w+@[a-z]+\.[a-z]{2,4}$/gi  //a very simple regex

btn.addEventListener("click",function(){
     if(regex.test(test.value)){
       result.innerHTML = "the value entered is correct ";
     }else{
       result.innerHTML = "the value entered is incorrect ";
     }
});

the problem here is that when i put email and click on validate button it works as expected but when you click second time on validate button it execute the else part which says the value entered is incorrect although there is still email present

0 Answers0