使用正则表达式验证邮件 [英] mail validation with regex

查看:166
本文介绍了使用正则表达式验证邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用正则表达式进行邮件验证,但它不工作。我的错误是哪里?
通常正常工作,但是当我放置正则表达式控件时,它看不到是,您输入的邮件将启用此正则表达式。



这是我的代码;



index.jsp

  ; HEAD> 
< title>邮件控制从Db< / title>
< script type =text / javascriptsrc =jquery-1.2.6.min.js>< / script>
< script type =text / javascriptsrc =check.js>< / script>
< / head>

< body>

< div align =left>
< form name =chkFormid =chkFormmethod =post>
< table>
< tr>
< td>电子邮件< / td>
< td>
< input type =textname =emailid =emailsize =20>< / td>
< td>
< div id =emailInfoalign =left>< / div>
< / td>
< / tr>

< / table>
< / form>
< / div>

< / body>
< / html>

check.js


$ b $ ($($)$($)$($($)

var myForm = $(#chkForm),email = $ ),emailInfo = $(#emailInfo);


//发送ajax请求以检查电子邮件
email.blur(function()
{
$ .ajax({
type:POST,
data:email =+ $(this).attr(value),
url:check。 jsp,

beforeSend:function()
{
emailInfo.html(< font color ='blue'> Kontrol Ediliyor ..< / font> );
},// end beforeSend:function()



成功:function(data)
{
var reg = /\S+@\S+\.\S+/;


if(reg.test(email.val()))
{
var checkData = data.toString();
if(checkData == 0)
{
emailok = false;
emailInfo.html(< font color ='red'>使用中的邮件< / font>);
} // end if(checkData == 0)
else if(checkData == 1)
{
emailok = true;
emailInfo.html(< font color ='green'>邮件未使用< / font>);
} // end else if(checkData == 1)

} // end if(reg.test(email.val()))

else
{
emailInfo.html(< font color ='red'>ınvalidmail< / font>);
} // end else
} // end success:function(data)


});
}); // end email.blur(function()
}); // end $(document).ready(function()
pre>

我在check.jsp中有问题并解决了问题。


  1. 问题是关于regex.Regex是false。

  2. 条件是假的,我用if(reg.test(email.val()))更改它。


解决方案

你的正则表达式只允许大写字母,所以TEXT@EXAMPLE.COM可以没问题,但是test@example.com没有。您可以通过在正则表达式中添加不区分大小写的标记来更改。

  / ^ [A-Z0-9._ %+  - ] + @ [A-Z0-9 .-] + \。[AZ] {2,6} $ / i 

或者简单地允许正则表达式本身中的az

  / ^ [A-Za-z0- [A-Za-z] {2,6} $ / 
/ pre>

但请注意,检查电子邮件的有效性是相当困难的任务,因为允许的邮件地址的范围非常广泛,甚至可能包含特别-CHA rs喜欢äüö等等,所以你的正则表达式不完美,记住这一点!



在PHP中,我使用了这个功能多年,它总是似乎有效。



在这种情况下,您是否使用 .toString() .test()给你一个布尔值,你可以用 == false 完美检查 - 有没有什么理由你想将其转换成字符串?


I want to make mail validation with regex but it's not working.where is my mistake? It's normally working but when I put a regex control its never see yes the mail you key in is enable for this regex format.

Here is my code;

index.jsp

 <head>
<title>Mail Control From Db</title>
<script type="text/javascript" src="jquery-1.2.6.min.js"></script>
<script type="text/javascript" src="check.js"></script>
</head>

<body>

<div align="left">
<form name="chkForm" id="chkForm" method="post"  >
    <table>  
            <tr>
                    <td>Email</td>
                    <td>
                    <input type="text" name="email" id="email" size="20"  ></td>
                    <td>
                    <div id="emailInfo" align="left"></div>
                    </td>
            </tr>

    </table>
</form>
</div>

</body>
</html>

check.js

$(document).ready(function()
 {    
 var myForm = $("#chkForm"), email = $("#email"), emailInfo = $("#emailInfo");     


 //send ajax request to check email          
 email.blur(function()
 {          
 $.ajax({
    type: "POST",
    data: "email="+$(this).attr("value"),
    url: "check.jsp",

    beforeSend: function()
    {
        emailInfo.html("<font color='blue'>Kontrol Ediliyor..</font>");
    },//end beforeSend: function()



    success: function(data)
    {                   
        var reg = /\S+@\S+\.\S+/;


        if (reg.test(email.val()))
        {
            var checkData = data.toString();
                if(checkData == 0)
                {  
                    emailok = false;
                    emailInfo.html("<font color='red'>Mail in use</font>");
                }//end if(checkData == 0)
                else if(checkData == 1)
                {
                    emailok = true;
                    emailInfo.html("<font color='green'>Mail is not in use</font>");
                }//end else if(checkData == 1)

        }//end if (reg.test(email.val()))

        else
        {
            emailInfo.html("<font color='red'>ınvalid mail</font>");
        }//end else
  }// end success: function(data)


 });
 });//end email.blur(function()
 });//end $(document).ready(function()

I had a problem in check.jsp. and solved it.

  1. problem is about regex.Regex was false.
  2. condition was false. i change it with if (reg.test(email.val())).

解决方案

Your regex only allows capital letters, so TEXT@EXAMPLE.COM would be okay but test@example.com not. You can change that either by adding the case-insensitive-flag to your regex

/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,6}$/i

or simply allow a-z in the regex itself

/^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,6}$/

But keep in mind that checking emails for validity is quite a difficult tasks to do, as the range of allowed mail-adresses is extremely broad and they even could contain special-chars like äüö etc. So your regex is not perfect, keep that in mind!

In PHP, I used this function for years and it always seemed to work.

Why exactly are you using .toString() in this case? .test() gives you an boolean which you can perfectly check with == false - is there any reason why you want to convert this to a string?

这篇关于使用正则表达式验证邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆