英文:
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 -->
<form>
<input name="name" required pattern=".*\S+.*" title="不能只包含空白字符">
<input type="submit">
</form>
<!-- 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 -->
<form>
<input name="name" required pattern=".*\S+.*" title="can't include only whitespace">
<input type="submit">
</form>
<!-- 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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论