如何验证电子邮件和替代电子邮件?

时间:2016-02-04 07:29:38

标签: php jquery

如何在jquery中的同一个onchange上验证电子邮件和重复电子邮件?

<label class="labeln">Email Address: </label> 
<input type="email" required="" id="email" name="email" class="input1"> 
<label class="labeln">Repeat Email Address: </label> 
<input type="email" required="" id="remail" name="remail" class="input1">

3 个答案:

答案 0 :(得分:2)

您只需选择input1类,因为它们具有相同的类
HTML

<label class="labeln">Email Address: </label> 
<input type="email" required="" id="email" name="email" class="input1"> 
<label class="labeln">Repeat Email Address: </label> 
<input type="email" required="" id="remail" name="remail" class="input1">

<强>的Javascript

$('.input1').on('change',function(){
  //Your validation here
  //But use $(this).val() to get the value of the email

   if( !isValidEmailAddress( $(this).val() ) ) { 
    /* do stuff here */ 

   }
});
aSeptik

See this function

function isValidEmailAddress(emailAddress) {
    var pattern = /^([a-z\d!#$%&'*+\-\/=?^_`{|}~\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]+(\.[a-z\d!#$%&'*+\-\/=?^_`{|}~\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]+)*|"((([ \t]*\r\n)?[ \t]+)?([\x01-\x08\x0b\x0c\x0e-\x1f\x7f\x21\x23-\x5b\x5d-\x7e\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]|\\[\x01-\x09\x0b\x0c\x0d-\x7f\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))*(([ \t]*\r\n)?[ \t]+)?")@(([a-z\d\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]|[a-z\d\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF][a-z\d\-._~\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]*[a-z\d\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])\.)+([a-z\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]|[a-z\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF][a-z\d\-._~\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]*[a-z\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])\.?$/i;
    return pattern.test(emailAddress);
};

See also this 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);
}

答案 1 :(得分:2)

<强> HTML

<label class="labeln">Email Address: </label> 
<input class="vEmail" type="email" required="" id="email" name="email"> 
<label class="labeln">Repeat Email Address: </label> 
<input class="vEmail" type="email" required="" id="remail" name="remail">
<script src="/js/jquery.validate.min.js'"></script>
<script src="/js/additional-methods.min.js"></script>

<强>脚本

$( ".vEmail" ).change(function() {
    //validate confirm email using jquery validate
    $('#myform').validate({
        rules: {
            email: 'required',
            remail: {
                equalTo: '#email'
            }
        },
        messages: {
            email: {
                required: "Please enter email"
            },
            remail: {
                equalTo: "Confirm email is not match"
            }
        }
    });
}

答案 2 :(得分:0)

我建议使用Validator-https://github.com/chriso/validator.js,语法很简单:

validator.isEmail('foo@bar.com');

这是我广泛使用(并做出了贡献)的JavaScript库,其中包括针对常见的免费邮件提供商(如Gmail和Yahoo)的自定义逻辑。这些提供者有自己的规则,验证者也要负责。

可以使用简单的正则表达式(如其他答案所建议的那样),但是电子邮件语法实际上相当复杂,因此最好使用更复杂的东西。

如果您要确保电子邮件地址是真实的并且可以接受传入的电子邮件,也可以使用email verification API like Kickbox(免责声明-我在那儿工作,我们很棒)。