jQuery form validation not working
I'm struggling with my form validation here:
// JavaScript Document
$(document).ready( function() {
$("#submit").click(function(){
// set initial error value to false
var hasError = false;
// set var for each form field
var order_ref = $("#order_ref").val();
var order_name = $("#order_name").val();
var order_telephone = $("#order_telephone").val();
var order_email = $("#order_email").val();
var order_message = $("#order_message").val();
var order_size = $("#sendEmail input.radio:checked").val();
// validate each of them
if(order_ref == '') { $("#order_ref").addClass('haserror'); hasError = true; }
if(order_name == '') { $("#order_name").addClass('haserror'); hasError = true; }
if(order_telephone == '') { $("#order_telephone").addClas开发者_如何学JAVAs('haserror'); hasError = true; }
if(order_email == '') { $("#order_email").val().addClass('haserror'); hasError = true; }
//if(order_message == '') { $("#order_message").val().addClass('haserror'); hasError = true; }
if(order_size == '') { $("#order_size").val().addClass('haserror'); hasError = true; }
// if there are errors
if(hasError == true) {
alert('whoa!');
return false;
};
// if there's no errors, proceed
if(hasError == false) {
$.post("/photo/theme/foodphoto/includes/mail_send.php",
{
// pass each of the form values to the PHP file for processing
order_ref: order_ref,
order_name: order_name,
order_telephone: order_telephone,
order_email: order_email,
order_message: order_message,
order_size: order_size
},
function(data){
// no errors? great now do what you want to show the user his message is sent
$('#sendEmail, .faded').slideUp(500);
$('.thanks').delay(500).fadeIn(500);
}
);
};
return false;
});
});
Can you spot what I'm doing wrong?
Thank you! :)
These lines are wrong:
if(order_email == '') { $("#order_email").val().addClass('haserror'); hasError = true; }
if(order_size == '') { $("#order_size").val().addClass('haserror'); hasError = true; }
remove the val()
Copy/paste error it looks like :)
精彩评论