当我运行时,没有任何内容出现。这些函数应该验证我的条目,以确保用户输入正确的字符。但是,无论我在字段中输入什么条目,都不会出现错误。我怎样才能解决这个问题?我的javascript代码嵌入到我的HTML代码中。我也有问题,我试图通过执行form.email.value从HTML脚本获取电子邮件条目,但它在密码和用户名工作上做类似的事情时不起作用。我该如何解决这个问题?
我有这段代码:
echo <<< MFILE
<html>
<title> Verify </title>
<body>
<header id = "header">
<h1 align="center">Login</h1>
<p align="center">Javascript Verification.</p>
</header>
<script>
function validateAll(form)
{
alert(form.username.value)
var hold = validateEmail(form.email.value)
hold = hold + validateUsername(form.username.value)
hold = hold + validatePassword(form.password.value)
if(hold =="")
{
alert("Correct entries!")
return true
}
else
{
alert(hold)
return false
}
}
function validateUsername(field) {
if (field == "") return "No Username was entered.\n"
else if (field.length < 5)
return "Usernames must be at least 5 characters.\n"
else if (/[^a-zA-Z0-9_-]/.test(field))
return "Only a-z, A-Z, 0-9, - and _ allowed in Usernames.\n"
return ""
}
function validatePassword(field) {
if (field == "") return "No Password was entered.\n"
else if (field.length < 6)
return "Passwords must be at least 6 characters.\n"
else if (!/[a-z]/.test(field) || !/[A-Z]/.test(field) || !/[0-9]/.test(field))
return "Passwords require one each of a-z, A-Z and 0-9.\n"
return ""
}
function validateEmail(field) {
if (field == "") return "No Email was entered.\n"
else if (!((field.indexOf(".") > 0) && (field.indexOf("@") > 0)) || /[^a-zA-Z0-9.@_-]/.test(field))
return "The Email address is invalid.\n"
return ""
}
</script>
<form method="post" name="valid" enctype="multipart/form-data" onsubmit="return validateAll(this)">
<div >
<label for="email"><b>Email</b></label>
<input type="text" placeholder="Email" name="email" required>
</div>
<div >
<label for="username"><b>Username</b></label>
<input type="text" placeholder="Username" name="username" required>
</div>
<div >
<label for="password"><b>Password</b></label>
<input type="password" placeholder="Password" name="password" required>
</div>
<input type="submit" value="Login" name="submit">
</form>
MFILE;
echo "</body></html>";
?>