html - How can I make my input border colors red when they fail validation? (Vanilla JavaScript only) - Stack Overflow

So I have a few text inputs on a form, and I'd like to make the borders glow red when the user tri

So I have a few text inputs on a form, and I'd like to make the borders glow red when the user tries to submit the form and the inputs do not meet a specific criteria. The criteria is that the fields cannot be empty, and if they are, the form will take the users back to the field and show a red border.

Below is what I've got so far, but not only do the styles not show up, but the fields disappear.

What do I need to change?

const submitButton = document.getElementById('submitButton');
const name = document.getElementById('name');
const email = document.getElementById('email');

function nameValidation() {
    if (name.value == 0) {
        name.style.borderColor = "red solid 5px";
    }
}

function emailValidation() {
    if (email.value == 0) {
        email.style.borderColor = "red solid 5px";
    }
}

submitButton.addEventListener('click', () => {
    nameValidation();
    emailValidation();
});
<form action="index.html" novalidate>
      <fieldset>
      
        <label for="name">Name:</label>
          <input type="text" id="name">

        <label for="mail">Email:</label>
          <input type="email" id="email">
          
      </fieldset>
      
      <button id="submitButton">Register</button>
</form>

So I have a few text inputs on a form, and I'd like to make the borders glow red when the user tries to submit the form and the inputs do not meet a specific criteria. The criteria is that the fields cannot be empty, and if they are, the form will take the users back to the field and show a red border.

Below is what I've got so far, but not only do the styles not show up, but the fields disappear.

What do I need to change?

const submitButton = document.getElementById('submitButton');
const name = document.getElementById('name');
const email = document.getElementById('email');

function nameValidation() {
    if (name.value == 0) {
        name.style.borderColor = "red solid 5px";
    }
}

function emailValidation() {
    if (email.value == 0) {
        email.style.borderColor = "red solid 5px";
    }
}

submitButton.addEventListener('click', () => {
    nameValidation();
    emailValidation();
});
<form action="index.html" novalidate>
      <fieldset>
      
        <label for="name">Name:</label>
          <input type="text" id="name">

        <label for="mail">Email:</label>
          <input type="email" id="email">
          
      </fieldset>
      
      <button id="submitButton">Register</button>
</form>

Share Improve this question asked Nov 7, 2020 at 20:47 user14474885user14474885
Add a ment  | 

1 Answer 1

Reset to default 2

HTML Buttons have a default behaviour, due to which it refreshes the entire page and that is the reason why your form vanishes. To prevent that, you just need to use event.preventDefault() like I've done below in the Submit button event handler function.

Secondly, the reason your CSS doesn't apply is because you are trying to add '5px solid' to the border-color CSS property. border-color only accepts color as input. If you need to set other properties you need to do it on border.

Also, you are paring the value of name.value and email.value to 0. You should instead do it with empty string ''. However, empty string is a falsy value in Javascript, so even if you just say name.value and value is empty string ('') then it will be false in an if condition. All you need to do is if(!name.value) i.e. if name.value is not true, then execute condition.

const submitButton = document.getElementById('submitButton');
const name = document.querySelector('#name');
const email = document.querySelector('#email');

function nameValidation() {
  if (!name.value) {
    name.style.border = '5px solid';
    name.style.borderColor = 'red';
  }
}

function emailValidation() {
  if (!email.value) {
    email.style.border = '5px solid';
    email.style.borderColor = 'red';
  }
}

submitButton.addEventListener('click', e => {
  e.preventDefault();
  nameValidation();
  emailValidation();
});
<form action="index.html" novalidate>
          <fieldset>
            <label for="name">Name:</label>
            <input type="text" id="name" />

            <label for="mail">Email:</label>
            <input type="email" id="email" />
          </fieldset>

          <button id="submitButton">Register</button>
        </form>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745298297a4621276.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信