Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript text form validation from another field being updated by same form

I created below form: when you enter a name in first text box, it dynamically adds the names to another field below after pressing the + button. The function is implemented on the + button.

Now I want to add a validation logic within the same script, so that same name shouldn't be added twice. Please advise, only want to implement using javascript.

    function promptAdd(list){
    var text = "";
    var inputs = document.querySelectorAll("input[type=text]");
    for (var i = 0; i < inputs.length; i++) {
        text += inputs[i].value;
        }
    var li = document.createElement("li");
    var node = document.createTextNode(text);
    li.appendChild(node);
    document.getElementById("list").appendChild(li);

        }
<!doctype html>
<html>
<div class="row">
    <div class="col-lg-6 mb-1">
        <div class="card h-100 text-left">
            <div class="card-body">
                <h4 class="card-title">Add Resources</h4>
                <input type="text" class="form-control" name="employee" placeholder="Enter Name" />
                <small id="message" class="form-text text-muted">Press + to add to your list</small>
                <button id="bd1" class="btn add-more" onclick="promptAdd(list)" type="button">+</button>
                <br></br>
                <h5>List of Resources added</h5>
                <div class="form-control" id="list">
                    <span id="list">
                </div>
            </div>
        </div>
    </div>
</div>
</html>
like image 956
tx-911 Avatar asked Jun 18 '26 15:06

tx-911


2 Answers

The validation could be implemented simply by looping through all the li's and comparing the text of every li with the value of the input and if the values matches just return false, like :

var lis = document.querySelectorAll('#list li');
for (var i = 0; i < lis.length; i++) {
  if (lis[i].innerText == text) {
    return false;
  }
}

Hope this helps.

function promptAdd(list) {
  var text = "";
  var inputs = document.querySelectorAll("input[type=text]");
  
  for (var i = 0; i < inputs.length; i++) {
    text += inputs[i].value;
  }
  
  var lis = document.querySelectorAll('#list li');
  for (var i = 0; i < lis.length; i++) {
    if (lis[i].innerText == text ){
      resetInputs();
      
      return false;
    }
  }
  
  var li = document.createElement("li");
  var node = document.createTextNode(text);
    
  li.appendChild(node);
  document.getElementById("list").appendChild(li);
  
  resetInputs();
}

function resetInputs(){
  var inputs = document.querySelectorAll("input[type=text]");
  
  for (var i = 0; i < inputs.length; i++) {
    inputs[i].value = "";
  }
}
<div class="row">
  <div class="col-lg-6 mb-1">
    <div class="card h-100 text-left">
      <div class="card-body">
        <h4 class="card-title">Add Resources</h4>
        <input type="text" class="form-control" name="employee" placeholder="Enter Name" />
        <small id="message" class="form-text text-muted">Press + to add to your list</small>
        <button id="bd1" class="btn add-more" onclick="promptAdd(list)" type="button">+</button>
        <br><br>
        <h5>List of Resources added</h5>
        <div class="form-control" id="list">
          <span id="list"></span>
        </div>
      </div>
    </div>
  </div>
</div>
like image 183
Zakaria Acharki Avatar answered Jun 21 '26 04:06

Zakaria Acharki


Loop though all li elements and check their innerText with the new text. If you want to ignore capitalization you can use innerText.toUpperCase() === newText.toUpperCase()

function promptAdd(list) {
  var text = "";
  var inputs = document.querySelectorAll("input[type=text]");

  for (var i = 0; i < inputs.length; i++) {
    text += inputs[i].value;
  }

  if (textAlreadyExistsInList(text)) {
    return;
  };

  var li = document.createElement("li");
  var node = document.createTextNode(text);
  li.appendChild(node);
  document.getElementById("list").appendChild(li);
};

function textAlreadyExistsInList(text) {
  var itemExists = false;
  var items = document.getElementById("list").querySelectorAll('li');

  for (var i = 0; i < items.length; i++) {
    if (items[i].innerText === text) { //to ignore casing: items[i].innerText.toUpperCase() === text.toUpperCase()
      itemExists = true;
      break;
    }
  }

  return itemExists;
}
<div class="row">
  <div class="col-lg-6 mb-1">
    <div class="card h-100 text-left">
      <div class="card-body">
        <h4 class="card-title">Add Resources</h4>
        <input type="text" class="form-control" name="employee" placeholder="Enter Name" />
        <small id="message" class="form-text text-muted">Press + to add to your list</small>
        <button id="bd1" class="btn add-more" onclick="promptAdd(list)" type="button">+</button>
        <br></br>
        <h5>List of Resources added</h5>
        <div class="form-control" id="list">

        </div>
like image 31
Nope Avatar answered Jun 21 '26 05:06

Nope



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!