jeudi 26 mars 2020

function does not work within if statement in javascript [closed]

As a newbie I'm trying to hide or show a div when a statement has occured. Instead of using an alert, I want a div to appear. So I've created a var

let demo = document.getElementById("alert").style.visibility = "visible";

If I use this var in my statement nothing seems to happen, even if I log the var. How can I resolve this?

 function numberValidation()
  {
    let cardno = /^(?:5[1-5][0-9]{14})$/;
    let str = cardNumber.value;
    let results = str.match(/\d{4}/g);
    let final_cc_str = results.join(" ");
    let demo = document.querySelector(".alert");
    demo.style.visibility="visible"
  
    if(str.match(cardno) || (isNaN(str))|| (str.length < 16))
        {
         
        alert("Please enter 16 digits, no characters allowed");
        console.log(demo);
        }
      else
        {
         
          alert(final_cc_str);
        }
 <div class="alert">
      <span class="closebtn" onclick="this.parentElement.style.display='none';">&times;</span> 
      <strong>Invalid input</strong> Please enter 16 digits, no characters allowed.
    </div>

Aucun commentaire:

Enregistrer un commentaire