Home > Article > Web Front-end > JS does not use regular expressions to verify whether the input string is empty (contains spaces) (graphic tutorial)
Below I will bring you a JS implementation code that does not use regular expressions to verify whether the input string is empty (containing spaces). Let me share it with you now and give it as a reference for everyone. Follow the editor to come and take a look
In the project, I need to verify whether the input string is empty, including spaces. I don’t like to use regular expressions, so I thought of the indexOf function of js. The indexOf() method can return The first occurrence of a specified string value in the string. If the string value to be retrieved does not appear, the method returns -1.
Syntax: stringObject.indexOf(searchvalue,fromindex), searchvalue is required, fromindex: optional parameter, the position at which the search starts in the string. Its legal values are 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("内容不能包含空格!"); return false; } if (enumValue== "") { alert("请输入内容!"); return false; } }
The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.
Related articles:
javascript ES6 has added an introduction to the use of let command (graphic tutorial)
javascript event model (Picture and text tutorial)
Detailed answer to the usage of document.referrer in JavaScript (code attached)
The above is the detailed content of JS does not use regular expressions to verify whether the input string is empty (contains spaces) (graphic tutorial). For more information, please follow other related articles on the PHP Chinese website!