如果电子邮件有效,请创建条件

时间:2018-01-27 14:00:36

标签: jquery

在发布到数据库之前检查电子邮件是否为avalid格式时,我遇到了麻烦。我无法弄清楚如何编写条件,以便表单在有效时发送,但如果无效则不发送。

真的很感激任何指针。感谢

的jQuery

          // on post comment click 
            $('#bt-add-com').click(function(){
                var theCom = $('.the-new-com');
                var theName = $('#name-com');
                var theMail = $('#mail-com');

    //Check inputs are not empty
                if( !theCom.val()){ 
                    swal("Error", "You need to write a comment!", "error");


                 if( !theName.val()){ 
                    swal("Error", "You need to write a name!", "error");

    //Check email is valid

    function validateEmail(theMail){
        var emailReg = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
        var valid = emailReg.test(theMail);

        if(!valid) {
            return false;
        } else {
            return true;
        }
    }

    if(validateEmail(theMail)){
         //E
//EMAIL IS VALID, NOW SEND
    } else {
         alert("Email is not correct format return false.");
//EMAIL IS INVALID DO NOT SEND THE
    }

      }
      }else{ 


                    $.ajax({
                        type: "POST",
                        url: "comments/ajax/add-comment2.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-bt').fadeIn();
                                $('.response').fadeIn().append(html); 

    $('.new-com-cnt').hide('slow');//fade out form
    swal("Success", "Your comment is posted", "success");


                        }  
                    });
                }
            });

        });
          });

0 个答案:

没有答案