博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
如何使用JavaScript检查输入是否为空
阅读量:2519 次
发布时间:2019-05-11

本文共 3742 字,大约阅读时间需要 12 分钟。

by Zell Liew

由Zell Liew

如何使用JavaScript检查输入是否为空 (How to check if an input is empty with JavaScript)

Last week, I shared how to . Today, let’s talk about the same thing, but with JavaScript.

上周,我分享了如何 。 今天,让我们谈谈同一件事,但使用JavaScript。

It’s much simpler.

这要简单得多。

Here’s what we’re building:

这是我们正在构建的:

验证输入的事件 (Events to validate the input)

If you want to validate the input when a user types into the field, you can use the input event.

如果要在用户在字段中键入内容时验证输入,则可以使用input事件。

const input = document.querySelector('input')input.addEventListener('input', evt => {  // Validate input})

If you want to validate the input when a user submits a form, you can use the submit event. Make sure you prevent the default behavior withpreventDefault.

如果要在用户提交表单时验证输入,则可以使用submit事件。 确保使用preventDefault防止默认行为。

If you don’t prevent the default behavior, browsers will navigate the user to the URL stated in the action attribute.

如果您不阻止默认行为,浏览器将把用户导航到action属性中指定的URL。

const form = document.querySelector('form')form.addEventListener('submit', evt => {  evt.preventDefault()
// Validate input})

验证输入 (Validating the input)

We want to know whether an input is empty. For our purpose, empty means:

我们想知道输入是否为空。 就我们的目的而言,空表示:

  1. The user hasn’t typed anything into the field

    用户尚未在该字段中输入任何内容
  2. The user has typed one or more empty spaces, but not other characters

    用户键入了一个或多个空格,但没有键入其他字符

In JavaScript, the pass/fail conditions can be represented as:

在JavaScript中,通过/失败条件可以表示为:

// Empty' ''  ''   '
// Filled'one-word''one-word '' one-word'' one-word ''one phrase with whitespace''one phrase with whitespace '' one phrase with whitespace'' one phrase with whitespace '

Checking this is easy. We just need to use the trim method. trim removes any whitespace from the front and back of a string.

检查很容易。 我们只需要使用trim方法。 trim删除字符串前后的所有空格。

const value = input.value.trim()

If the input is valid, you can set data-state to valid. If the input is invalid, you can set the data-state to invalid.

如果输入有效,则可以将data-state设置为valid 。 如果输入无效,则可以将data-state设置为invalid

// This is JavaScript
input.addEventListener('input', evt => {  const value = input.value.trim()
if (value) {    input.dataset.state = 'valid'  } else {    input.dataset.state = 'invalid'  }})
/* This is CSS */
/* Show red borders when filled, but invalid */input[data-state="invalid"] {  border-color: hsl(0, 76%, 50%);}
/* Show green borders when valid */input[data-state="valid"] {  border-color: hsl(120, 76%, 50%);}This isn’t the end yet. We have a problem.

When a user enters text into the field, input validation begins. However, if the user removes all text from the field, the input continues to be invalid.

当用户在字段中输入文本时,输入验证开始。 但是,如果用户从字段中删除了所有文本,则输入将继续无效。

We don’t want to invalidate the input if the user removes all text. They may need a moment to think, but the invalidated state sets off an unnecessary alarm.

如果用户删除了所有文本,我们不想使输入无效。 他们可能需要花点时间思考,但是无效状态会引发不必要的警报。

To fix this, we can check whether the user has entered any text into the input before we trim it.

要解决此问题,我们可以在trim之前检查用户是否在输入中输入了任何文本。

input.addEventListener('input', evt => {  const value = input.value
if (!value) {    input.dataset.state = ''    return  }
const trimmed = value.trim()
if (trimmed) {    input.dataset.state = 'valid'  } else {    input.dataset.state = 'invalid'  }})

Here’s a Codepen for you to play with:

这是一个Codepen供您使用:

See the Pen by Zell Liew () on .

见笔通过泽尔与Liew( )上 。

Thanks for reading. Did this article help you out? If it did, I hope you consider . You might help someone else out. Thanks so much!

谢谢阅读。 这篇文章对您有帮助吗? 如果确实如此,希望您考虑 。 您可能会帮助别人。 非常感谢!

This article was originally posted at .Sign up for my if you want more articles to help you become a better frontend developer.

本文最初发布在 如果您想获得更多文章来帮助您成为更好的前端开发人员,请注册我的 。

翻译自:

转载地址:http://xerwd.baihongyu.com/

你可能感兴趣的文章
linux系统目录结构
查看>>
学习进度
查看>>
使用Postmark测试后端存储性能
查看>>
NSTextView 文字链接的定制化
查看>>
第五天站立会议内容
查看>>
ATMEGA16 IOport相关汇总
查看>>
JAVA基础-多线程
查看>>
面试题5:字符串替换空格
查看>>
[Codevs] 线段树练习5
查看>>
Amazon
查看>>
component-based scene model
查看>>
Echart输出图形
查看>>
hMailServer搭建简单邮件系统
查看>>
从零开始学习jQuery
查看>>
Spring+SpringMVC+MyBatis深入学习及搭建(四)——MyBatis输入映射与输出映射
查看>>
opacity半透明兼容ie8。。。。ie8半透明
查看>>
CDOJ_24 八球胜负
查看>>
Alpha 冲刺 (7/10)
查看>>
一款jQuery打造的具有多功能切换的幻灯片特效
查看>>
SNMP从入门到开发:进阶篇
查看>>