您的表单数据不是那么大,如果您想学习新东西而不是在 javascript 端使用插件和验证,您可以手动执行某些操作(我认为)。
好吧,我更喜欢通过服务器端来验证,看看这个:
测试和工作:
索引.htm
<html>
<head>
<title>Get more information</title>
</head>
<style>
.error {
color: red;
}
</style>
<script src="http://code.jquery.com/jquery-1.7.2.min.js" type="text/javascript"></script>
<script>
$(document).ready(function(){
$('#emailbutton').on('click', function(){
var fname = $('#fname').val();
var lname = $('#lname').val();
var email = $.trim($('#email').val());
$.post('validate.php', {fname:fname, lname:lname, email:email}, function(data){
if(data != 1) {
var obj = $('#' + data + '_msg');
if(data == 'fname') {
obj.html('Invalid first name.').addClass('error').show().fadeOut('slow');
} else if (data == 'lname') {
obj.html('Invalid last name.').addClass('error').show().fadeOut('slow');
} else if (data == 'email') {
obj.html('Invalid email.').addClass('error').show().fadeOut('slow');
}
} else if(data == 1) {
$('#registerform').submit();
}
});
});
});
</script>
<body>
<form id="registerform" action="thanks.php" method="POST">
<ul id="inputform">
<li>
<label for="firstname" id="firstnamelabel">First Name</label>
<input type="text" name="first_name" id="fname" class="registerboxes" />
<span id="fname_msg"></span>
</li>
<li>
<label for="lastname" id="lastnamelabel">Last Name</label>
<input type="text" name="last_name" id="lname" class="registerboxes" />
<span id="lname_msg"></span>
</li>
<li>
<label for="email" id="emaillabel">E-mail Address</label>
<input type="text" name="emailbox" id="email" class="registerboxes" />
<span id="email_msg"></span>
</li>
</ul>
<input type="button" value="Submit" id="emailbutton" />
</form>
</body>
</html>
验证.php
<?php
if(isset($_POST['fname']) && isset($_POST['lname']) && isset($_POST['email'])) {
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$email = $_POST['email'];
if(validateInput($fname, 2, 25)) {
if(validateInput($lname, 2, 30)) {
if(validateEmail($email, 5, 150)) {
print(1);
} else {
print('email');
}
} else {
print('lname');
}
} else {
print('fname');
}
}
function validateInput($value, $minlength, $maxlength) {
$valid = false;
try {
if (strlen(trim($value)) == 0) {
$valid = false;
} else if (strlen(trim($value)) < $minlength || strlen(trim($value)) > $maxlength) {
$valid = false;
} else {
$valid = true;
}
} catch (exception $e) {
/* get exception: $e->getMessage() */
$valid = false;
}
return $valid;
}
function validateEmail($email, $minlength, $maxlength) {
$valid = false;
try {
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$valid = false;
} else if (strlen(trim($email)) == 0) {
$valid = false;
} else if (strlen(trim($email)) < $minlength || strlen(trim($email)) > $maxlength) {
$valid = false;
} else {
$valid = true;
}
} catch (exception $e) {
/* get exception: $e->getMessage() */
$valid = false;
}
return $valid;
}
?>
谢谢.php
<?php
if(isset($_POST['first_name']) && isset($_POST['last_name']) && isset($_POST['emailbox'])) {
$fname = $_POST['first_name'];
$lname = $_POST['last_name'];
$email = $_POST['emailbox'];
echo $fname.' '.$lname.' '.$email;
// Do what you need with your data at this point, don't forget to sanitize values to insert them in your DB :-)
}
?>