Thursday, 22 August 2013

Jquery form won't validate to success

Jquery form won't validate to success

I'm having problem to "get pass" the validation of my form validator, it
stops and won't start to process. When it should be processed it gives me
"You need to fill out: "
What could be wrong?
Fidde her: http://jsfiddle.net/SBYQ5/
Jquery:
$("form").submit(function (e) {
e.preventDefault(); // This will prevent the form submission
var response = "";
var missing = "";
$('#submit-holder').find('input').each(function(){
if ($(this).val() == '') {
missing += $('label[for="' + this.id + '"]').html() + " | ";
empty_fields = true;
missing_fields = true;
}
else if ($(this).val() != '') {
empty_fields = false;
$('.alert').hide();
}
});
response += "* <strong>You need to fill out:</strong> " + missing;
if(!$('#user_terms').is(':checked'))
{
response += "<br>* You need to accept the terms and conditions to
continue";
}
var email = $('[name="txtEmail"]').val();
if(IsEmail(email)==false){
valid_email = false;
response += "<br><strong>* The e-mail address you've entered is
invalid</strong>";
}
response_message(response);
if(!missing_fields && valid_email !== false &&
$('#user_terms').is(":checked")) {
$('.alert').hide()
}
});
// Functions:
function IsEmail(email) {
var regex =
/^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
if(!regex.test(email)) {
return false;
}else{
return true;
}
}
function response_message(response) {
$('.alert').addClass('alert-danger');
$('.alert').fadeIn();
$('.error_message').html(response);
}

No comments:

Post a Comment