JS does not use regular validation to validate whether the input string is empty of contains implementation code for whitespace

  • 2021-06-28 10:48:08
  • OfStack

In your project, you need to verify that the input string is empty, including spaces, and you don't like to use regularity, so you think of the indexOf function of js. The indexOf() method returns the first occurrence of a specified string value in the string. If the string value to be retrieved does not appear, it returns -1.

Syntax: stringObject.indexOf (searchvalue, fromindex), searchvalue required, fromindex: optional parameter, the location in the string where the retrieval begins.Its legal value is 0 to stringObject.length-1.If omitted, the first character of the string is retrieved.


    function CheckValue() {

      var enumValue = document.getElementById('txtEnumValue').value;

      if (enumValue.indexOf(" ")>=0) {

        alert(" Content cannot contain spaces! ");

        return false;


      if (enumValue== "") {

        alert(" Please enter content! ");

        return false;



Related articles: