gpt4 book ai didi

JavaScript 表单验证错误不打印

转载 作者:太空宇宙 更新时间:2023-11-04 02:51:30 25 4
gpt4 key购买 nike

我将以下代码用于我用来验证的表单,并希望在出现错误时在字段下方显示错误:

<!DOCTYPE html>
<html>
<head>
<style type="text/css">

.errorcss {
background-color: yellow;
color:red;
}
</style>

<script type="text/javascript">
error=" ";

function isBlank(s) {

var len=s.length
var i
for(i=0; i<len; ++i) {
if(s.charAt(i) != " ") return false
}
return true
}

function validate(fieldName, fieldValue) {

if(isBlank(fieldValue)) {
error = fieldName + " cannot be left blank.";
alert(fieldName+" cannot be left blank.")
return false
}
return true
}

function validatePass(passwordValue, confirmPasswordValue) {

if (passwordValue !== confirmPasswordValue) {
alert("Password and Confirm Password do not match")
return false
}
return true
}


function validateForm() {

if(!validate("The last name field", document.contest.last.value))
return false
if(!validate("The email field", document.contest.email.value))
return false
if(!validate("The password field", document.contest.pass.value))
return false

if(!validatePass(document.contest.pass.value, document.contest.repass.value))
return false
if(!validate("The description field", document.contest.desc.value))
return false
}

</script>
</head>

<body>
<form name="contest" onSubmit = "return validateForm()" method="GET">
<h2 align="center">Sign Up Form</h2>
<p>
*Last Name:<input type="text" name="last" size="16">
First Name:<input type="text" name="first" size="12">
Middle Initial:<input type="text" name="initial" size="2">
</p>
<div class="errorcss">
<script type="text/javascript">document.write(error);</script>
</div>
<p>
*E-mail Address:<input type="email" name="email">
*Password:<input type="password" size="10" name="pass">
*Confirm Password:<input type="password" size = "10" name="repass">
</p>
<p>
In 50 words or less, describe yourself:
</p>
<textarea name="desc" ROWS="5" COLS="40"></textarea>
<p>
Submit your form:<input type="SUBMIT" value="Submit my form">
</p>
</form>
</body>
</html>

我面临的问题是,我希望在带有 errorcss 类的 div 中显示没有姓氏错误。但它没有显示它。此外,我想知道如何使用 JavaScript 和 CSS 显示所有错误。

最佳答案

我添加了 15 个分号,删除了错误 div 中的脚本,为 errordiv 指定了一个 id ('errordiv'),并添加了:

document.getElementById('errordiv').innerText=error;

在函数 validateForm(); 中。

结果代码:

<!DOCTYPE html>
<html >
<head >
<style type="text/css" >

.errorcss {
background-color : yellow;
color : red;
}
</style >

<script type="text/javascript" >
error = " ";

function isBlank( s )
{

var len = s.length;
var i;
for ( i = 0; i < len; ++i ) {
if ( s.charAt( i ) != " " ) return false;
}
return true;
}

function validate( fieldName, fieldValue )
{

if ( isBlank( fieldValue ) ) {
error = fieldName + " cannot be left blank.";
alert( fieldName + " cannot be left blank." );
return false;
}
return true;
}

function validatePass( passwordValue, confirmPasswordValue )
{

if ( passwordValue !== confirmPasswordValue ) {
alert( "Password and Confirm Password do not match" );
return false;
}
return true;
}

function validateForm()
{

if ( !validate( "The last name field", document.contest.last.value ) ) {
document.getElementById( 'errordiv' ).innerText = error;
return false;
}
if ( !validate( "The email field", document.contest.email.value ) ) {
return false;
}
if ( !validate( "The password field", document.contest.pass.value ) ) {
return false;
}

if ( !validatePass( document.contest.pass.value, document.contest.repass.value ) ) {
return false;
}
if ( !validate( "The description field", document.contest.desc.value ) ) {
return false;
}
}

</script >
</head >

<body >
<form name="contest" onSubmit="return validateForm()" method="GET" >
<h2 align="center" >Sign Up Form</h2 >

<p >
*Last Name:<input type="text" name="last" size="16" > First Name:<input type="text" name="first" size="12" > Middle Initial:<input type="text" name="initial" size="2" >
</p >

<div class="errorcss" id="errordiv" >

</div >
<p >
*E-mail Address:<input type="email" name="email" > *Password:<input type="password" size="10" name="pass" > *Confirm Password:<input type="password" size="10" name="repass" >
</p >

<p >
In 50 words or less, describe yourself:
</p >
<textarea name="desc" ROWS="5" COLS="40" ></textarea >

<p >
Submit your form:<input type="SUBMIT" value="Submit my form" >
</p >
</form >
</body >

关于JavaScript 表单验证错误不打印,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32906175/

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