【发布时间】:2021-12-07 23:25:09
【问题描述】:
我在提交之前编写了一个 Javascript 来验证我的表单。 Javascript 的功能是防止我的表单在用户提供的电话号码与我的 Javascript 中的结果匹配但显示一条错误消息“电话号码已使用!”时自行提交。
我的问题是验证工作正常:用户收到他们提供的电话号码已被使用的警报,但我的表单在显示错误消息后仍会自行提交。
我做错了什么?
下面是我的代码。
function check(form) /*function to check used phone number*/ {
/*the following code checkes whether the entered phone number is matching*/
if (form.phonenum.value == "0807575566464"
|| form.phonenum.value == "09057487463")
{
alert("Phone number used! provide a new one! \n") /*displays error message*/
} else if (form.phonenum.value.length<11 || form.phonenum.value.length>11) { alert("Phone number should be 11 digits! \nAnd it should begin with; 080, 081, 070, 090, or 091.");
}
else {
return false;
}
}
<form action='' method='POST' enctype="multipart/form-data" id="formName">
<input type="text" id="phonenum" name="myinput">
<button href='/' type='submit' onclick="check(this.form)">Submit</button>
</form>
【问题讨论】:
-
这能回答你的问题吗? JavaScript code to stop form submission
-
@tbs 是的,但你能帮我一个忙,更正我的 javascript。我是业余爱好者。谢谢。
标签: javascript html forms validation