[英]JavaScript form validation - function not running on submit.
I'm currently working on some very basic validation for a form that posts a bunch of information from a web form to an SQL database with a simple PHP script. 我目前正在为表单进行一些非常基本的验证,该表单使用简单的PHP脚本将大量信息从Web表单发布到SQL数据库。 The PHP script is working fine, and the values are being posted to the database, however the validation I have written appears to be ignored.
PHP脚本运行正常,并且值已发布到数据库,但是我编写的验证似乎被忽略了。 I've attempted to fix it for a while now and can't see any glaring mistakes, but apologies if it's anything trivial.
我已经尝试修复了一段时间,看不到任何明显的错误,但是如果这很琐碎,则表示歉意。
HTML: 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: 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....
}
Exactly your code is working for me: 确实,您的代码对我有用:
Created following test.html file and opened in chrome: 创建以下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>
Submitting an emtpy form alerts: 提交空表单警报:
Sorry, your username must be between 1 and 20 characters long.
抱歉,您的用户名必须介于1到20个字符之间。
http://fiddle.jshell.net/hmariod/LRZkR/ http://fiddle.jshell.net/hmariod/LRZkR/
<form name = "registerForm" method = "POST" 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 = "button" onclick="ValidateRegistration();" class = "registerButton" value = "Register"></p>
</form>
function ValidateRegistration(){
try{
var username=document.forms["registerForm"]["username"].value;
if (username.length < 1)
{
alert("You must provide a username");
return;
}
if (username.length>20 || username.length<1)
{
alert("Sorry, your username must be between 1 and 20 characters long.");
return;
}
document.forms["registerForm"].submit();
}catch(er){
alert(er);
}
}
我会摆脱onsubmit中的回报。
Put your code inside try/catch blocks a see what's going on. 将您的代码放入try / catch块中,看看发生了什么。
try{
//your code
}catch(er){
alert(er);
}
i think the problem is in your comparing line 我认为问题出在您的比较线上
if (username==null || username==" ")
which should be 应该是
if (username==null || username=="")
i hope it solves your problem. 我希望它能解决您的问题。 please let me know ;-)
请告诉我 ;-)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.