只是试图让表单验证工作。 在你说任何我使用jquery将文件拉入div之前,所以css文件在index.php中(这里不相关)。
所以我试图在这里进行一些表单验证,因为我还在这里学习大学,并且我对这里的错误以及验证表单的脚本感到困惑。
哦,只是一个cookie问题......当有人提交表单时,它是否可能会引发内容保留在字段内的错误?或者我可以不,因为我使用js提交表单数据?
感谢任何回复:)
<html>
<head>
<script src="https://code.jquery.com/jquery-2.2.0.min.js"></script>
<script language="javascript" type="text/javascript">
function validateForm(form){
var user=form.username.value;
var email = form.email.value;
var str=form.password.value;
var cstr=form.cpassword.value;
var err={};
var validemail =/^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
//check required fields
//checks username
if((user.length < 3) || (user.length > 15)){
err.message="username must be between 3 and 15 characters.";
err.field=form.username;
}
//password should be minimum 4 chars but not greater than 8
if ((str.length < 4) || (str.length > 8)) {
err.message="Invalid password length";
err.field=form.password;
}
//check if repeated password matches
if(cstr!=str)
{
err.message="Passwords do not match";
err.field.form.cpassword;
}
//validate email
if(!(validemail.test(email))){
err.message="Invalid email";
err.field=form.email;
}
if(err.message)
{
document.getElementById('results').innerHTML = err.message;
err.field.focus();
return false;
}
return true
}
</script>
</head>
<body>
<div id="content" class="content">
<div class="content-heading">
<div class="content-heading-title">
<h3>Register now!</h3>
</div>
</div>
<div class="content-info">
<form id="myForm" method="post" onsubmit="return validateForm(this);">
Username: <input name="username" id="username" type="text" /><br />
Email: <input name="email" id="email" type="text" /><br />
Password: <input name="password" id="password" type="password" /><br />
Confirm Password:<input name="cpassword" id="cpassword" type="password" /><br />
<input type="button" id="submitFormData" onclick="SubmitFormData();" value="Submit" />
</form>
<div id="results"></div>
</div>
</div>
<script>
function SubmitFormData() {
var username = $("#username").val();
var email = $("#email").val();
var pass = $("#password").val();
var cpass = $("#cpassword").val();
$.post("submit.php", {
username: username,
email: email,
pass: pass,
cpass: cpass
}, function(data) {
$('#results').html(data);
$('#myForm').trigger('reset');
});
}
</script>
</body>
</html>
答案 0 :(得分:1)
删除表单上的 onsubmit ,然后从 SubmitFormData 中调用 validateForm 。
你应该这样做:
function validateForm(){
var username = $("#username").val();
var email = $("#email").val();
var pass = $("#password").val();
var cpass = $("#cpassword").val();
var err={
message: "",
field: ""
};
var validemail =/^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
//check required fields
//checks username
if(username.length < 3 || username.length > 15){
err.message="username must be between 3 and 15 characters.";
err.field="username";
}
//password should be minimum 4 chars but not greater than 8
if (pass.length < 4 || pass.length > 8) {
err.message="Invalid password length";
err.field="password";
}
//check if repeated password matches
if(cpass!=pass)
{
err.message="Passwords do not match";
err.field="cpassword";
}
//validate email
if(!validemail.test(email)){
err.message="Invalid email";
err.field="email";
}
if(err.message != "")
{
$("#results").html(err.message);
$("#"+err.field).focus();
return false;
}
$("#results").html(""); // to clean results div in the case of previous error message
return true;
}
function SubmitFormData() {
if(validateForm()) {
var username = $("#username").val();
var email = $("#email").val();
var pass = $("#password").val();
var cpass = $("#cpassword").val();
$.post("submit.php", {
username: username,
email: email,
pass: pass,
cpass: cpass
}, function(data) {
$('#results').html(data);
$('#myForm').trigger('reset');
});
}
}
答案 1 :(得分:1)
<html>
<head>
<script src="https://code.jquery.com/jquery-2.2.0.min.js"></script>
</head>
<body>
<div id="content" class="content">
<div class="content-heading">
<div class="content-heading-title">
<h3>Register now!</h3>
</div>
</div>
<div class="content-info">
<form id="myForm" method="post">
Username: <input name="username" id="username" type="text" /><br />
Email: <input name="email" id="email" type="text" /><br />
Password: <input name="password" id="password" type="password" /><br />
Confirm Password:<input name="cpassword" id="cpassword" type="password" /><br />
<input type="button" id="submitFormData" onclick="SubmitFormData();" value="Submit" />
</form>
<div id="results"></div>
</div>
</div>
<script>
function SubmitFormData() {
var username = $("#username").val();
var email = $("#email").val();
var pass = $("#password").val();
var cpass = $("#cpassword").val();
var validemail = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
if((username.length < 3) || (username.length > 15)) { //Useranme should be minimum 3 chars but not greater than 15
var message = "username must be between 3 and 15 characters.";
$('#results').html(message);
$("#username").focus();
return false;
} else if ((pass.length < 4) || (pass.length > 8)) { //password should be minimum 4 chars but not greater than 8
var message = "Invalid password length";
$('#results').html(message);
$("#password").focus();
return false;
} else if(cpass != pass) { //check if repeated password matches
var message = "Passwords do not match";
$('#results').html(message);
$("#cpassword").focus();
return false;
} else if(email == "") { //check if email address enterd
var message = "Enter Email address";
$('#results').html(message);
$("#email").focus();
return false;
} else if(!validemail.test(email)){ //validate email
var message = "Invalid email";
$('#results').html(message);
$("#email").focus();
return false;
} else { // Then POST the Form
$('#results').html("Sending Registration");
$.post("submit.php", {
username: username,
email: email,
pass: pass,
cpass: cpass
}, function(data) {
$('#results').html(data);
$('#myForm').trigger('reset');
});
}
}
</script>
</body>
</html>
删除表单上的onsubmit并删除Validation函数并将所有验证放在 theSubmitFormData()函数中,如上面的代码
答案 2 :(得分:0)
需要解释的很多事情和缺陷。但是我通过小修改扩展了你的代码。剩下的你可以理解和学习。 改变如下:
<html>
<head>
<script src="https://code.jquery.com/jquery-2.2.0.min.js"></script>
<script language="javascript" type="text/javascript">
function validateForm(form){
var user=form.username.value;
var email = form.email.value;
var str=form.password.value;
var cstr=form.cpassword.value;
var err={};
var validemail =/^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
//check required fields
//checks username
if((user.length < 3) || (user.length > 15)){
err.message = "username must be between 3 and 15 characters.<br><br>";
err.field=form.username;
}
//password should be minimum 4 chars but not greater than 8
if ((str.length < 4) || (str.length > 8)) {
err.message += "Invalid password length.<br><br>";
err.field=form.password;
}
//check if repeated password matches
if(cstr!=str)
{
err.message += "Passwords do not match.<br><br>";
err.field.form.cpassword;
}
//validate email
if(!(validemail.test(email))){
err.message += "Invalid email.<br><br>";
err.field=form.email;
}
if(err.message)
{
document.getElementById('results').innerHTML = err.message;
err.field.focus();
return false;
} else {
SubmitFormData();
return false;
}
}
</script>
</head>
<body>
<div id="content" class="content">
<div class="content-heading">
<div class="content-heading-title">
<h3>Register now!</h3>
</div>
</div>
<div class="content-info">
<form id="myForm" method="post" onsubmit="return validateForm(this);">
Username: <input name="username" id="username" type="text" /><br />
Email: <input name="email" id="email" type="text" /><br />
Password: <input name="password" id="password" type="password" /><br />
Confirm Password:<input name="cpassword" id="cpassword" type="password" /><br />
<input type="submit" id="submitFormData" value="Submit" />
</form>
<div id="results"></div>
</div>
</div>
<script>
function SubmitFormData() {
var username = $("#username").val();
var email = $("#email").val();
var pass = $("#password").val();
var cpass = $("#cpassword").val();
$.post("submit.php", {
username: username,
email: email,
pass: pass,
cpass: cpass
}, function(data) {
$('#results').html(data);
$('#myForm').trigger('reset');
});
}
</script>
</body>
</html>