gpt4 book ai didi

可选移动端的 JavaScript 验证

转载 作者:行者123 更新时间:2023-11-28 08:25:03 25 4
gpt4 key购买 nike

我有一个注册表单,其中有一个 MobileNo 字段,用户应该在其中提供他们的手机号码,但该字段是可选的,因为用户可以选择不提供他/她的手机号码。
在数据库表中,我保留了该字段的ALLOW NULL 选项。

现在我想在此字段上使用 JavaScript 验证,以便用户只能输入一个整数值,并且只有 10 位数字(如果他/她选择提供他/她的手机号码)。
问题是,当我为此编写代码时,当我将移动字段保留为空时,它会在 JavaScript 验证正在处理时显示错误。

我的 JavaScript 代码如下:

function f1() {
if (document.form2.mobile.length!=10 || isNan(document.form2.mobile) {
alert("Mobile No must be NUMERIC and 10 digit long");
return false;
}

是否有任何解决方案,因为这些验证仅在用户选择输入他/她的手机时才有效,否则如果为空,注册将完成。

最佳答案

将其包装在另一种情况下,如下所示:

function f1() {
if(document.form2.mobile.value !== "") {
if (document.form2.mobile.length!=10 || isNan(document.form2.mobile)
{
alert("Mobile No must be NUMERIC and 10 digit long");
return false;
}
}
}

关于可选移动端的 JavaScript 验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22539128/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com