javascript - 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;
// }
}
解决方案
您在此区块中的一半支票:
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()
它以删除任何前导或尾随空格,然后进行检查。
同样,这很可能发生在客户端,因此如果您将其用于任何重要的事情,您可能需要对服务器进行额外检查。
您应该能够根据您的示例进行调整
推荐阅读
- python - itertools.cycle(iterable) vs while True
- android - 应用安装后的 Android 9 奇怪行为
- swift - How to import one image and set it as a UIIMageView. Then again with another UIIMageView?
- javascript - 如何选择有限(最多 5 张)多张图片在 HTML/PHP 中?
- vb.net - VB.net 文本索引
- bash - 用于在字段级别区分 CSV 文件并将结果输出到 CSV 以保持颜色的工具
- gtk3 - Gtk.MenuItem 内的按钮不可点击
- swift - AppCoordinator delegate through view controllers
- c# - Setting HttpRequestMessage.Content (json) for post SendAsync results in Unexpected character error
- android - 在 build-gradle 中更改编译仍然错误