首页 > 解决方案 > Javascript空字段验证不起作用

问题描述

我想进行一些空字段输入验证,但它们根本不起作用。我试过使用.length,等于0,等于null,但没有一个能完成这项工作。知道为什么吗?这是代码:

if (this.subscriberform.callForward.onBusy) {
        console.log(
          "MSISDNOnBusy",
          this.subscriberform.callForward.msisdnOnBusy
        );
        if (this.subscriberform.callForward.msisdnOnBusy) {
          if (this.subscriberform.profile.starDial.callForward) {
            if (
              this.subscriberform.callForward.msisdnOnBusy.match(
                this.subscriberform.starDial.regex
              ) === null
            ) {
              this.errorMessageAlert(
                "Busy MSISDN does not match call forwarding regular expressions."
              );
              // console.log(this.errorMessageAlert)
              // return;
            } 
            if (this.subscriberform.callForward.msisdnOnBusy==="" || this.subscriberform.callForward.msisdnOnBusy===null
            || this.subscriberform.callForward.msisdnOnBusy===0 || this.subscriberform.callForward.msisdnOnBusy.length===""
            || this.subscriberform.callForward.msisdnOnBusy.length===0 || this.subscriberform.callForward.msisdnOnBusy.length==null
            || this.subscriberform.callForward.msisdnOnBusy.length==0 || this.subscriberform.callForward.msisdnOnBusy.length==""
            || this.subscriberform.callForward.msisdnOnBusy.length==null || this.subscriberform.callForward.msisdnOnBusy=="" 
            || this.subscriberform.callForward.msisdnOnBusy==null || this.subscriberform.callForward.msisdnOnBusy==0
            ) {
              this.errorMessageAlert(
                "Busy MSISDN must not be empty"
                );
              return;
            }
          }
        }
        // else {
        //   this.errorMessageAlert("Busy MSISDN must not be empty.");
        //   return;
        // }
      }

标签: javascriptvalidation

解决方案


您在此区块中的一半支票:

if (this.subscriberform.callForward.msisdnOnBusy==="" || this.subscriberform.callForward.msisdnOnBusy===null
            || this.subscriberform.callForward.msisdnOnBusy===0 || this.subscriberform.callForward.msisdnOnBusy.length===""
            || this.subscriberform.callForward.msisdnOnBusy.length===0 || this.subscriberform.callForward.msisdnOnBusy.length==null
            || this.subscriberform.callForward.msisdnOnBusy.length==0 || this.subscriberform.callForward.msisdnOnBusy.length==""
            || this.subscriberform.callForward.msisdnOnBusy.length==null || this.subscriberform.callForward.msisdnOnBusy=="" 
            || this.subscriberform.callForward.msisdnOnBusy==null || this.subscriberform.callForward.msisdnOnBusy==0
            )

真的没什么意思。

假设这msisdnOnBusy是一个字符串,在您的示例开始时,您将进行以下检查:

if (this.subscriberform.callForward.msisdnOnBusy) {

所以它已经是“真实的”,所以你最后的大 if 块永远不会返回真实。(https://developer.mozilla.org/en-US/docs/Glossary/Truthy

由于空字符串在 js 中被认为是“假的”,msisdnOnBusy它包含一些东西 - 一个简单的console.log()会告诉你什么

编辑问题“我如何更改对空字段的验证?”

在我可以肯定地说之前,我需要更多地了解你想要做什么,callForward是什么,如果msisdnOnBusy实际上是一个字符串。一般来说,第一步是确保 HTML 是正确的。例如,将required属性添加到您的输入中,如下所示:

<input type="text" id="username" name="username" required>

意味着您获得了基本的表单验证,除非username填写,否则它不会提交。

但是,显然我们不能信任客户端,因此需要进行一些额外的验证:

const username = document.getElementById('username').value.trim();
if(!username) {
    console.error(`No username; show an alert`)
}

在这里,我们获取username输入的值,调用trim()它以删除任何前导或尾随空格,然后进行检查。

同样,这很可能发生在客户端,因此如果您将其用于任何重要的事情,您可能需要对服务器进行额外检查。

您应该能够根据您的示例进行调整


推荐阅读