Reputation: 1
I usually don't ask questions in places like this as its filled with information that I probably could find that will solve my problem. However, I am new to PHP, Javascript etc.. and I just can't seem to grasp this problem I am having, so I turn to you guys and would greatly appreciate the help.
So, I am playing about with a comment system a fellow created but wanted to as a Javascript function that would check the validation of an email. Here is the code as followed...
$(function () {
//alert(event.timeStamp);
$('.new-com-bt').click(function (event) {
$(this).hide();
$('.new-com-cnt').show();
$('#name-com').focus();
});
/* when start writing the comment activate the "add" button */
$('.the-new-com').bind('input propertychange', function () {
$(".bt-add-com").css({
opacity: 0.6
});
var checklength = $(this).val().length;
if (checklength) {
$(".bt-add-com").css({
opacity: 1
});
}
});
/* on clic on the cancel button */
$('.bt-cancel-com').click(function () {
$('.the-new-com').val('');
$('.new-com-cnt').fadeOut('fast', function () {
$('.new-com-bt').fadeIn('fast');
});
});
// on post comment click
$('.bt-add-com').click(function () {
var theCom = $('.the-new-com');
var theName = $('#name-com');
var theMail = $('#mail-com');
var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9] {2,4})+$/;
if (!theCom.val()) {
alert('oh! Don`t forget your message!');
}
if (!filter.test(theMail.value)) {
alert('Please provide a vaild email address');
theMail.focus;
return false;
} else {
$.ajax({
type: "POST",
url: "ajax/add-comment.php",
data: 'act=add-com&id_post=' + <? php echo $id_post; ? > +'&name=' + theName.val() + '&email=' + theMail.val() + '&comment=' + theCom.val(),
success : function (html) {
theCom.val('');
theMail.val('');
theName.val('');
$('.new-com-cnt').hide('fast', function () {
$('.new-com-bt').show('fast');
$('.new-com-bt').before(html);
})
}
});
}
});
});
As you can see I provided a var filter etc... but explaining in brief terms, the end result is it will alert to say the email is invalid but when typing in a valid email and trying to submit, no matter what it just comes up with the alert and the end result, which is meant to happen simply doesn't.
Sorry if I going to long way round about this or not having any knowledge of this, but i am willing to assist in anyway.
Again any help would be much appreciated.
Thanks
Upvotes: 0
Views: 107
Reputation: 685
You might want to consider using a validation library like parsley.js if you will be validating multiple input types. I prefer parsley, but there are several libraries available. (note: parsley relies on jquery).
<form id="emailForm" data-parsley-validate>
<label for="email">Email * :</label>
<input type="email" name="email" required />
</form>
By default, parsley will show a generic error message like "This field is required. This should be a valid email.", but you can easily customize the message, or opt-out of displaying any message at all.
data-parsley-error-message="my message" // A global error message for this input
data-parsley-email-message="my message" // Replace the default parsley email error message for this input
Upvotes: 0
Reputation: 1792
A useful function.
function validateEmail(email) {
var re = /^(([^<>()[\]\\.,;:\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,}))$/;
return re.test(email);
}
// Example
if(validateEmail("[email protected]"))
{
alert("Valid!");
}else{
alert("Invalid!");
}
Upvotes: 4