如何从“if-else”语句中排除空格 – JS

huangapple go评论55阅读模式
英文:

how to exlude empty spaces from "if-else" statments - JS

问题

I searched for how to exclude empty spaces like " " or " " or " " from an if statement but I didn't find any relevant topic.

I have an HTML page which adds a value to an array every time the user types in a value inside the input field, but I want to exclude the spaces. For example, if the user enters only empty spaces, I don't want them to be pushed inside the values[] array.

This is my JavaScript code:

const values = [];
const inputEL = document.getElementById("field-input");
if (inputEL.value) {
  values.push(inputEL.value);
  inputEL.value = "";
}

I tried this, but it didn't work as expected. The user can still enter empty spaces, and they are pushed inside the array:

if (!inputEL.value || inputEL.value === " ") {
  console.log("field input is empty / user entered empty spaces");
} else {
  values.push(inputEL.value);
  inputEL.value = "";
}
英文:

I searched for how to exclude empty spaces like " " or " " or " " from an if statments but i didnt find any relevant topic

i have an html page which add a value to an array everytime the user types in a value inside the input field
but i want to exclude the spaces, for example if the user enter only empty spaces, i dont want them to be pushed inside the values[] array

this is my jscode :

const values = []
const inputEL = document.getElementById("field-input")
  if (inputEL.value){
    values.push(inputEL.value)
    inputEL.value = ""
  }

I tried this but it didn't work as expected, user still can enter empty spaces and they are pushed inside the array:

if (!inputEL.value || inputEL.value === " "){
    console.log("field input is  empty / user enter empty spaces")
else{
    values.push(inputEL.value)
    inputEL.value = ""
}

答案1

得分: 1

你可以使用 trim() 函数来移除字符串开头和结尾的空格。

所以,如果输入是 " ",你将使用 trim() 将其变为 "",这是一个在你的 if 语句中被视为假值的值:

const values = []
const inputEL = document.getElementById("field-input");
const trimmed = inputEl.value.trim();
if (trimmed){
    values.push(trimmed)
    inputEL.value = ""
}
英文:

You could use trim() to remove whitespaces from the beginnen and end of the string.

So if the input is " ", you well trim() it down to "" which is a falsy value in your if:

const values = []
const inputEL = document.getElementById("field-input");
const trimmed = inputEl.value.trim();
if (trimmed){
    values.push(trimmed)
    inputEL.value = ""
}

答案2

得分: 0

<!-- 可以使用模式和必需属性结合来满足要求 -->

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

&lt;form&gt;
  &lt;input name="name" required pattern=".*\S+.*" title="不能只包含空白字符"&gt;
  &lt;input type="submit"&gt;
&lt;/form&gt;

<!-- end snippet -->

然后,您还可以使用 input.validity.valid 来检查它是否有效。

英文:

You could also use a pattern and a required attribute to combine the requirement

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

&lt;form&gt;
  &lt;input name=&quot;name&quot; required pattern=&quot;.*\S+.*&quot; title=&quot;can&#39;t include only whitespace&quot;&gt;
  &lt;input type=&quot;submit&quot;&gt;
&lt;/form&gt;

<!-- end snippet -->

then you can also use input.validity.valid to check if it's valid or not

答案3

得分: -1

你可以使用 string.trim() 方法,它会返回一个去除了两端(开头和结尾)空格的新数组,而不会修改原始数组。

英文:

You can use string.trim() method, it return a new array with whitespaces removed from both ends, (start and end) without modifying the original one

huangapple
  • 本文由 发表于 2023年3月9日 21:02:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/75685004.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定