Javascript 空字段验证不起作用

时间:2021-05-06 08:27:03

标签: javascript validation

我想做一些空字段输入验证,但它们根本不起作用。我试过使用 .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;
        // }
      }

1 个答案:

答案 0 :(得分:0)

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

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 块永远不会返回 true。 (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() 以删除任何前导或尾随空格,然后进行检查。

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

你应该能够适应你的例子