gpt4 book ai didi

php - JavaScript 表单验证 - 函数未在提交时运行。

转载 作者:行者123 更新时间:2023-11-28 20:42:51 24 4
gpt4 key购买 nike

我目前正在对一个表单进行一些非常基本的验证,该表单使用一个简单的 PHP 脚本将大量信息从 Web 表单发布到 SQL 数据库。 PHP 脚本工作正常,并且值已发布到数据库,但是我编写的验证似乎被忽略。我已经尝试修复它有一段时间了,看不到任何明显的错误,但如果有任何微不足道的问题,我深表歉意。

HTML:

<form name = "registerForm" method = "POST" onsubmit = "return ValidateRegistration()" action = "createUser.php">
<p class = "register">Desired Username*</p>
<p class = "registerDesc">Between 1 and 20 characters long</p>
<input type = "text" name = "username" class = "register">
<p class = "register">Desired Password*</p>
<p class = "registerDesc">We won't force securty onto you; between 6 and 255 characters long. No other criteria.</p>
<input type = "password" name = "password" class = "register">
<p class = "register">Confirm Password*</p>
<input type = "password" name = "confirmPassword" class = "register">
<p class = "register">Email Adress*</p>
<input type = "text" name = "email" class = "registerEmail">
<p class = "register">Bio</p>
<p class = "registerDesc">Extra information such as hobbies, occupation, background information. Maximum 4096 characters. We can do this later.</p>
<textarea name = "bio" class = "registerBio"></textarea>
<p><input type = "submit" class = "registerButton" value = "Register"></p>
</form>

JavaScript:

function ValidateRegistration()
{
//username block
var username=document.forms["registerForm"]["username"].value;
if (username==null || username==" ")
{
alert("You must provide a username");
return false;
}
if (username.length>20 || username.length<1)
{
alert("Sorry, your username must be between 1 and 20 characters long.");
return false;
}
//etc....
}

最佳答案

您的代码正是为我工作:

创建以下 test.html 文件并在 Chrome 中打开:

<html>
<head>
<script>
function ValidateRegistration()
{
//username block
var username=document.forms["registerForm"]["username"].value;
if (username==null || username==" ")
{
alert("You must provide a username");
return false;
}
if (username.length>20 || username.length<1)
{
alert("Sorry, your username must be between 1 and 20 characters long.");
return false;
}
//etc....
}
</script>
</head>
<body>
<form name = "registerForm" method = "POST" onsubmit = "return ValidateRegistration()" action = "createUser.php">
<p class = "register">Desired Username*</p>
<p class = "registerDesc">Between 1 and 20 characters long</p>
<input type = "text" name = "username" class = "register">
<p class = "register">Desired Password*</p>
<p class = "registerDesc">We won't force securty onto you; between 6 and 255 characters long. No other criteria.</p>
<input type = "password" name = "password" class = "register">
<p class = "register">Confirm Password*</p>
<input type = "password" name = "confirmPassword" class = "register">
<p class = "register">Email Adress*</p>
<input type = "text" name = "email" class = "registerEmail">
<p class = "register">Bio</p>
<p class = "registerDesc">Extra information such as hobbies, occupation, background information. Maximum 4096 characters. We can do this later.</p>
<textarea name = "bio" class = "registerBio"></textarea>
<p><input type = "submit" class = "registerButton" value = "Register"></p>
</form>
</body>
</html>

提交空表单警报:

Sorry, your username must be between 1 and 20 characters long.

关于php - JavaScript 表单验证 - 函数未在提交时运行。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14076752/

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