In the project, you need to verify whether the input string is empty, including spaces. You don’t like to use regularity, so you think of js’ indexOf function. The indexOf() method can return the location where a specified string value appears for the first time in the string. If the string value to be retrieved does not appear, the method returns -1.
Syntax: stringObject.indexOf(searchvalue,fromindex), searchvalue required, fromdex: optional parameter, the location where the search starts in the string. Its legal value is 0 to stringObject.length - 1. If this parameter is omitted, the search will start from the first character of the string.
Demo: 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; } }The above JS implementation code that does not require regular verification of whether the string entered is empty (including spaces) is all the content I have shared with you. I hope it can give you a reference and I hope you can support Wulin.com more.