could you please help me with the code above?
I', trying to have some validation in there but the validation is not being executed and the form is submitted without the validation:
HTML:
<form method="post" action="" id="subscription-form">
<div class="form-group">
<input class="subscribe-email" type="email" name="email" placeholder="[email protected]" required>
<p><span class="error-message">Please enter a valid email</span></p>
</div>
<button id="" class="" type="submit"><span>Subscribe</span></button>
</form>
JS:
$(document).ready(function() {
$('#subscription-form').on('submit', function (e) {
var form = $(this);
var regexEmail = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
var url = "https://mycustomapi.net";
if (!$('.subscribe-email').val().match(regexEmail)) {
$('.error-message').hide();
$.ajax({
type: "POST",
url: url,
data: form.serialize(),
success: function (data) {
$('#subscribed-error').hide()
$('#subscribed-feedback').show()
$('#subscription-form').hide()
},
error: function () {
$('#subscribed-error').show()
$('#subscribed-feedback').hide()
$('#subscription-form').show()
}
});
console.log('email submitted');
e.preventDefault();
return false;
}
else {
$('.error-message').show();
e.preventDefault();
return false;
}
});
});
Thanks