gpt4 book ai didi

javascript - 在 JavaScript 中成功验证后我无法显示任何消息

转载 作者:行者123 更新时间:2023-12-03 07:35:26 25 4
gpt4 key购买 nike

在 JavaScript 中成功验证后,我无法显示任何消息,并且最后的 if 语句出错了。我想表明我们会在通过验证后回复您。

<script>
function validateForm() {
var x = document.forms["myForm"]["fname"].value;
if (x.length < 10) {
window.alert("The field cannot contain more than 10 characters!");
return false;
}
}
function validateForm() {
var k = document.forms["myForm"]["query"].value;
if (k.length < 25) {
window.alert("The field cannot contain atleast 25 characters!");
return false;
}
}
function validateForm() {
var k1 = document.forms["myForm"]["email"].value;
var atpos = k1.indexOf("@");
var dotpos = k1.lastIndexOf(".");
if (atpos < 1 || dotpos < atpos + 2 || dotpos + 2 >= k1.length) {
alert("Not a valid e-mail address");
return false;
}
if (k1.length < 10) {
window.alert("The field cannot contain atleast 25 characters!");
}
}
if (x && k && k1 !== true) {
this.x = x;
this.k = k;
this.k1 = k1;
window.alert("We will get back to you");
}
</script>

最佳答案

function validateForm() {
var isValid = true;
var x = document.forms["myForm"]["fname"].value;
if (x.length < 10){
window.alert("The field cannot contain more than 10 characters!");
isValid = false;
}

var k = document.forms["myForm"]["query"].value;
if (k.length<25) {
window.alert("The field cannot contain atleast 25 characters!");
isValid = false;
}

var k1= document.forms["myForm"]["email"].value;
var atpos = k1.indexOf("@");
var dotpos = k1.lastIndexOf(".");
if (atpos < 1 || dotpos < atpos + 2 || dotpos + 2 >= k1.length) {
alert("Not a valid e-mail address");
isValid = false;
}
if (k1.length < 10) {
window.alert("The field cannot contain atleast 25 characters!");
isValid = false;
}
if (!isValid) {
// you should take a look here.. what do you want to achieve?
this.x = x;
this.k = k;
this.k1 = k1;
window.alert("We will get back to you");
}
return isValid;
}

您的表单应使用 onsubmit 属性:

<form onsubmit="return validateForm()"></form>

看看http://jqueryvalidation.org/documentation/ 。您可以考虑开始学习jQuery或其他 js 框架。

关于javascript - 在 JavaScript 中成功验证后我无法显示任何消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35630764/

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