Home > Enterprise >  Bootstrap: how to add <br> tag in modal
Bootstrap: how to add <br> tag in modal

Time:05-02

everyone.

I am using bootstrap modal. I am going to design a login in function in the modal.

There is a function of data checking.

However, the word 'br' show directly in html. I just want a line break effect.

When the user name is empty and password is <6 letters,

My html show:

User Name can not be empry br At least 6 letters for password

It seems the br tag does not work.

<!doctype html>
<html lang="en">
  <head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="Website">
    <meta name="keywords" content="HTML, CSS, JavaScript">
    <meta name="author" content="Tom Fan">
    <!-- Bootstrap CSS -->

    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=League Gothic&family=Noto Sans TC:wght@100;300;400;500;700;900&family=Open Sans&family=Roboto:ital,wght@0,400;0,500;0,700;0,900;1,400;1,500;1,700;1,900&display=swap" rel="stylesheet">
    
    <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
    <link rel="stylesheet" href="reset.css">
    <link rel="stylesheet" href="style.css">
    
  </head>

  <body>

<!-- Button trigger modal -->
<button type="button"  data-bs-toggle="modal" data-bs-target="#exampleModal">
  Login in
</button>

<!-- Modal -->
<div  id="exampleModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
  <div >
    <div >
      <div >
        <p  id="exampleModalLabel">Login In</p>
        <button type="button"  data-bs-dismiss="modal" aria-label="Close"></button>
      </div>
      <div >

        <form>
        
          <label for="user_id">User Name</label>
          <input type="text" id="user_id">
          
          <label for="password">Passowrd</label>
          <input type="password" id="password">
          
          <div ></div>

        <div >
          <button type="button submit" >Login in</button>
        </div>
      </form>

      </div>
      
    </div>
  </div>
</div>

<!-- Optional JavaScript; choose one of the two! -->

<!-- Option 1: Bootstrap Bundle with Popper --->
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg OMhuP IlRH9sENBO0LRn5q 8nbTov4 1p" crossorigin="anonymous"></script>

<!-- Option 2: Separate Popper and Bootstrap JS -->
<!--
<script src="https://cdn.jsdelivr.net/npm/@popperjs/[email protected]/dist/umd/popper.min.js" integrity="sha384-7 zCNj/IqJ95wo16oMtfsKbZ9ccEh31eOz1HGyDuCQ6wgnyJNSYdrPa03rtR1zdB" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js" integrity="sha384-QJHtvGhmr9XOIpI6YVutG 2QOK9T ZnN4kzFN1RtK3zEFEIsxhlmWl5/YESvpZ13" crossorigin="anonymous"></script>
-->
<script src="function.js"></script>
</body>
</html>
.error {
    border: 3px solid red;
}

.errorMessage {
    margin: 0;
    margin-bottom: 20px;
    width: 100%;
    color: red;
}
const loginForm = document.querySelector('form');
const userName = document.querySelector('#user_id');
const userPassword = document.querySelector('#password');
const errorDiv = document.querySelector('.errorMessage');

loginForm.addEventListener('submit',(e)=>{
   e.preventDefault;
   console.log(userName.value);
   console.log(userPassword.value);
    //value check
    let errorMessage = ""
    let error = false

    if(!userName.value.length)
    {
        errorMessage  = 'User Name can not be empry<br>'
        error= true
    }
    if(error)
    {
        loginForm.user_id.classList.add('error')
        errorDiv.innerText = errorMessage
    }
    else
    {
        loginForm.user_id.classList.remove('error')
        errorDiv.innerText = ''
    }
    if(userPassword.value.length < 6)
    {
        errorMessage  = 'At least 6 letters for password'
        error= true
    }
    if(error)
    {
        loginForm.password.classList.add('error')
        errorDiv.innerText = errorMessage
    }
    else
    {
        loginForm.password.classList.remove('error')
        errorDiv.innerText = ''
    }
});

CodePudding user response:

errorDiv.innerText = errorMessage

You are changing the text, not the HTML. Change that to:

errorDiv.innerHtml = errorMessage
  • Related