Home > Web Front-end > JS Tutorial > body text

JS does not use regular expressions to verify whether the input string is empty (contains spaces) (graphic tutorial)

亚连
Release: 2018-05-21 09:46:13
Original
1110 people have browsed it

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;

      }

    }
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template