如何获取输入验证以查看表中是否已存在用户输入?

时间:2018-01-08 07:17:41

标签: javascript jquery html css validation

感谢您查看此问题。我试图通过在表中找到它时不允许它存储来验证输入值。我正在努力找到它存储的变量,以便我可以将其放入验证中。

这是我的代码:

    var myNodelist = document.getElementsByTagName("LI");
var i;
for (i = 0; i < myNodelist.length; i++) {
  var span = document.createElement("SPAN");
  var txt = document.createTextNode("\u00D7");
  span.className = "close";
  span.appendChild(txt);
  myNodelist[i].appendChild(span);
}

// Click on a close button to hide the current list item
var close = document.getElementsByClassName("close");
var i;
for (i = 0; i < close.length; i++) {
  close[i].onclick = function() {
    var div = this.parentElement;
    div.style.display = "none";
  }
}

// Add a "checked" symbol when clicking on a list item
var list = document.querySelector('ul');
list.addEventListener('click', function(ev) {
  if (ev.target.tagName === 'LI') {
    ev.target.classList.toggle('checked');
  }
}, false);

// Create a new list item when clicking on the "Add" button
function newElement() {
  var li = document.createElement("li");
  var inputValue = document.getElementById("myInput").value;
  var t = document.createTextNode(inputValue);
  li.appendChild(t);
  if (inputValue === '' || inputValue === ) {
    alert("You must write something!");
  } else {
    document.getElementById("myUL").appendChild(li);
  }
  document.getElementById("myInput").value = "";

  var span = document.createElement("SPAN");
  var txt = document.createTextNode("\u00D7");
  span.className = "close";
  span.appendChild(txt);
  li.appendChild(span);

  for (i = 0; i < close.length; i++) {
    close[i].onclick = function() {
      var div = this.parentElement;
      div.style.display = "none";
    }
  }
}

我试图将我的验证放在IF语句中。

3 个答案:

答案 0 :(得分:0)

使用jQuery .each()方法迭代<li> parent内的所有id="myUL"元素,以检查该值是否存在。

我添加了一个示例<ul id="myUL">元素来说明它是如何工作的。

这是改变的功能:

function newElement() 
{
  var li = document.createElement("li");
  var inputValue = document.getElementById("myInput").value;

  // if empty
  if (inputValue.length == 0)
  {
     alert("You must write something!");
  }
  else
  {
      var exists = false;

      // go through all 'li' child elements of parent with id 'myUL'
      $( "#myUL li" ).each(function( index )
      {
          // $( this ).text() is the text of the current 'li' ellement
          var currentLiText = $( this ).text();
          // search for the appended char, the ×
          var doesItContainX = currentLiText.indexOf('\u00D7');

         // if an × is found remove it
         if (doesItContainX !== false)
         {
             currentLiText = currentLiText.substr(0, doesItContainX, currentLiText.length);
         }

         // compare the current li value to the inputValue
         // if they are the same, mark that a match was found
         if(currentLiText.toLowerCase() == inputValue.toLowerCase()) exists = true;
    });

    // if we didn't find a match add it
    if(!exists)
    {
        document.getElementById("myUL").appendChild(li);
        var t = document.createTextNode(inputValue);
        li.appendChild(t);
    }
    else
    {
        // we found a match report error
        alert('the value already exists');
    }
}

以下是代码:

(运行底部的代码段)

&#13;
&#13;
var myNodelist = document.getElementsByTagName("LI");
var i;
for (i = 0; i < myNodelist.length; i++) {
  var span = document.createElement("SPAN");
  var txt = document.createTextNode("\u00D7");
  span.className = "close";
  span.appendChild(txt);
  myNodelist[i].appendChild(span);
}

// Click on a close button to hide the current list item
var close = document.getElementsByClassName("close");
var i;
for (i = 0; i < close.length; i++) {
  close[i].onclick = function() {
    var div = this.parentElement;
    div.style.display = "none";
  }
}

// Add a "checked" symbol when clicking on a list item
var list = document.querySelector('ul');
list.addEventListener('click', function(ev) {
  if (ev.target.tagName === 'LI') {
    ev.target.classList.toggle('checked');
  }
}, false);

// Create a new list item when clicking on the "Add" button
function newElement() 
{
  var li = document.createElement("li");
  var inputValue = document.getElementById("myInput").value;

  // if empty
  if (inputValue.length == 0)
  {
     alert("You must write something!");
  }
  else
  {
      var exists = false;

      // go through all 'li' child elements of parent with id 'myUL'
      $( "#myUL li" ).each(function( index )
      {
           // $( this ).text() is the text of the current 'li' ellement
           var currentLiText = $( this ).text();
           // search for the appended char, the ×
           var doesItContainX = currentLiText.indexOf('\u00D7');
           
           // if an × is found remove it
           if (doesItContainX !== false)
           {
               currentLiText = currentLiText.substr(0, doesItContainX, currentLiText.length);
           }
           
           // compare the current li value to the inputValue
           // if they are the same mark that a match was found
           if(currentLiText.toLowerCase() == inputValue.toLowerCase()) exists = true;
           // for case-insensitive remove the .toLowerCase()
      });

      // if we didn't find a match add it
      if(!exists)
      {
          document.getElementById("myUL").appendChild(li);
          var t = document.createTextNode(inputValue);
          li.appendChild(t);
      }
      else
      {
          // we found a match report error
          alert('the value already exists');
      }
  }

  document.getElementById("myInput").value = "";

  var span = document.createElement("SPAN");
  var txt = document.createTextNode("\u00D7");
  span.className = "close";
  span.appendChild(txt);
  li.appendChild(span);

  for (i = 0; i < close.length; i++)
  {
    close[i].onclick = function()
    {
      var div = this.parentElement;
      div.style.display = "none";
    }
  }
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul id="myUL">
  <li>America</li>
  <li>asia</li>
  <li>germany</li>
  <li>Italy</li>
  <li>Canada</li>
</ul>

<input type="text" id="myInput" value="" />

<input type="button" value="click to add element" onclick="newElement()" />
&#13;
&#13;
&#13;

答案 1 :(得分:0)

Ypu需要在newElement之前添加另一个方法,如下所示。

var eleExist = function(eleValue){
  var lis = document.getElementById("myUL").getElementsByTagName("li");
  return lis.find(x => x.value.toLowerCase() === eleValue.toLowerCase());
}

然后

function newElement() { 
  ....
  ....
     var inputValue = document.getElementById("myInput").value;
      if (inputValue === '' || eleExist(inputValue) ) {
        alert("You must write something!");
      }
}

上面的东西应该有效。请注意,我没有尝试,因此您可能需要做一些测试。

答案 2 :(得分:0)

对于内存有效但不是时间有效的方法,您可以添加一个函数来验证并在if语句中调用它:

function newElement() {
  var li = document.createElement("li");
  var inputValue = document.getElementById("myInput").value;
  var t = document.createTextNode(inputValue);
  li.appendChild(t);
  if (inputValue === ''|| notDuplicated(inputValue) ) {
    alert("You must write something and you cannot add the same place 
  twice!");
  } else {
    document.getElementById("myUL").appendChild(li);
  }
  // etc...
}
//add this 
function notDuplicated (text) {
  //list of all previously entered values
  var list = document.getElementById("myUL").getElementsByTagName("li");;
  for (var item of list) {
    if (item.value === text) {
    //text value already inserted before
    return true;
    }
  }
  //this means the value was not entered before
  return false
}

或者我喜欢的方式 为了节省时间而不是内存效率方式,您可以轻松地将值存储在对象内 - 更多地使用内存但您可以跳过添加一个新的验证功能:

//make sure this one is global not inside the function
window.insertedTexts = {};
function newElement() {
  var li = document.createElement("li");
  var inputValue = document.getElementById("myInput").value;
  var t = document.createTextNode(inputValue);
  li.appendChild(t);
  //check if we have it in our new object
  if (inputValue === ''|| window.insertedTexts[inputValue] ) {
    alert("You must write something and you cannot add the same place 
  twice!");
  } else {
    //and add each new value to the object you created
    window.insertedTexts[inputValue] = true;
    document.getElementById("myUL").appendChild(li);
  }
  // etc...
}