0

I want to be able to perform validation based on how many characters were entered by the user - I want there to be a minimum of 7 characters. (The maximum value is set using an HTML attribute) - I have tried the following:

v3 = document.getElementById("npo-registration-number");
flag3 = true;

if (val >= 3 || val == 0) {
        if (v3.value == "") {
            v3.style.borderColor = "red";
            flag3 = false;
        }
        else if (v3.value.length === 7){
            v3.style.borderColor = "green";
            flag3 = true;
        }
   }

The above works to an extent. The input fields border colour will only show green if 7 characters are inputted. However, if i delete characters from that point onwards, the border remains green. Any help on the matter is appreciated.

1
  • Code is working as written. You say make it red when empty, make it green when length is 7. Where is the logic for numbers in between? What is supposed to happen? Commented Nov 5, 2020 at 18:45

5 Answers 5

4

I'm not entirely sure about what you want; is this close to what you are looking for?

You probably did this and didn't include it in your snippet, but we need this to run each time the form is edited. We add an event listener to the input.

const input = document.getElementById('npo-registration-number');
input.addEventListener('input', () => {
    // set the border to red if the value is < 7 characters
    if (input.value.length < 7) {
        input.style.borderColor = 'red';
        return;
    }
    // otherwise, set it to green
    input.style.borderColor = 'green';
});

This fixes an issue: you do not set the color of the border to red unless the value of the form is an empty string. Rather, we want the border to be red whenever the input length goes below seven.

Sign up to request clarification or add additional context in comments.

1 Comment

to improve your answers, try to always add some explanation in plain text
1

It's because of your condition

if (v3.value == "") {
            v3.style.borderColor = "red";
            flag3 = false;
        }

When you start deleting characters in input, your input is not becoming red again because It's red only when there are no characters and it change to green after 7 characters inputed.

Comments

1

You need to capture the input event so js can evaluate and update. https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/input_event

     const ev3 = document.getElementById("npo-registration-number");
     let flag3 = true;
     ev3.addEventListener('input', (e) => {
       const val = e.target.value;
       if (val.length === 7){
                ev3.style.borderColor = "green";
                flag3 = true;          
        }
        else {
           ev3.style.borderColor = "red";
           flag3 = true;   
        }
     });
input {
  outline: 0;
  border: 1px solid;
}
<input id="npo-registration-number" type='text'>

Comments

1

You can just use button that will run this function for it, like send or post buttons(many sites use this method).

function click(){
  v3 = document.getElementById("npo-registration-number");
  flag3 = true;

  if (val >= 3 || val == 0) {
        if (v3.value == "") {
            v3.style.borderColor = "red";
            flag3 = false;
        }
        else if (v3.value.length === 7){
            v3.style.borderColor = "green";
            flag3 = true;
        }
   }
}

Comments

-1

Try this please

v3 = document.getElementById("npo-registration-number");
flag3 = true;

if (v3.value == "") {
   v3.style.borderColor = "red";
   flag3 = false;
 } else {


 if (v3.value.length < 7){
    v3.style.borderColor = "red";
    flag3 = false;
    }
 else {
    v3.style.borderColor = "green";
    flag3 = true;
  }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.