【问题标题】:Email Validation in Javascript Before AJAXAJAX 之前 Javascript 中的电子邮件验证
【发布时间】:2015-12-20 06:01:44
【问题描述】:

所以我得到了这个用于表单提交的 js 代码,一切正常,但是,我不知道在代码中的何处以及写什么,因此电子邮件得到了验证检查。我应该在哪里写什么来验证检查电子邮件?

$(document).ready(function() {
  $("#submit").click(function() {

    var name = $("#fullname2").val();
    var email = $("#fullemail2").val();
    var state = $("#selectstate").val();
    // Returns successful data submission message when the entered information is stored in database.
    var dataString = 'FullName=' + name + '&email=' + email + '&SovereignState=' + state;
    if (name == '' || email == '' || state == '') {
      $('#required_fields').show();
    } else {
      // AJAX Code To Submit Form.
      $.ajax({
        type: "POST",
        url: "demo.php",
        data: dataString,
        cache: false,
        success: function(phpSays) {
          if (phpSays == "OK") {
            $('#email_error').show();
            $('#required_fields').hide();
          } else {
            $('#sinatra2').hide();
            $('#thanks').fadeIn(1000);
            $('#spreading_message').delay(1800).fadeIn(1500);
            $('#by_social').delay(3000).fadeIn(1500);
            $('#email_error').hide();
            $('#required_fields').hide();
          }
        }
      });
    }
    return false;
  });
});

【问题讨论】:

标签: javascript jquery ajax forms validation


【解决方案1】:

查看您的代码,我可以建议以下方法来说明您可以在哪里进行电子邮件验证

if(name==''||email==''||state=='')
{
   $('#required_fields').show();
}//this is fine
else if(!valid(email))//call a function which validates email and returns true or false
{
    //Display the message either with same $('#required_fields') changing the text or 
    //Add one more field to display invalid email message
}
else
{
    //Your ajax call here
}

现在你的有效函数看起来像

function valid(email)
{
    var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
    return emailReg.test(email); //this will either return true or false based on validation
}

【讨论】:

  • 谢谢,但你能解释一下这个功能吗?我需要写一个 if 语句(if 'true)吗?
  • 没关系,我刚刚意识到它可以完美运行,谢谢!
  • No.. emailReg.test 为您进行验证.. 随时.. 快乐编码.. :)
【解决方案2】:

验证的更好地方是您有“fullemail2”输入字段的地方。即使在 javascript 文件中,您也应该在创建 dataString 之前执行此操作。这样您就可以在提交之前进行验证。

【讨论】:

    【解决方案3】:
     $(document).ready(function(){
     $("#submit").click(function(){
    
     var name = $("#fullname2").val();
     var email = $("#fullemail2").val();
     var state = $("#selectstate").val();
     // Returns successful data submission message when the entered information is stored in database.
     var dataString = 'FullName='+ name + '&email='+ email + '&SovereignState='+ state;
     if(name==''||email==''||state=='')
     {
     $('#required_fields').show();
     }
     else
     {
     // AJAX Code To Submit Form.
       // <-- email address should be here
       ...........    
     }
     return false;
     });
     });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-04-06
      • 2011-10-20
      • 2011-02-16
      • 1970-01-01
      • 2012-11-03
      • 2012-09-15
      • 2012-08-23
      相关资源
      最近更新 更多