English 中文(简体)
javascript regex——与我的非常简单的格格格格格格格格格格外错了什么?
原标题:javascript regex - whats wrong with my very simple regex?

我想以html的形式验证电子邮件地址。 我很少了解这方面的情况。

审定非常简单,仅与any_symbols@any_symbols. 2_to_car_symbols格式相匹配。

这里是试图使用<代码>+@.+.{2,8}$的Im。 然而,它的确做了大量工作,它验证了模式any_symbols@four_symbols

注:不担心这种简单的验证,在服务器一米处,filter_var(php)上,并寄给电子邮件。 • 在投入电子邮件地址时,只需要使纽顿能够形成某种衰弱模式:

EDIT Those patterns "any_symbols..." I ve mentioned in question are just textual representation of what I m trying to input. This is not what I type in input field :) Usually I type "[email protected]", or "[email protected]" and etc. :)

<><><><>>>>>> 实际代码:

   var email_regex = new RegExp("^.+@.+..{2,8}$");
if ($target.val().match(email_regex) !== null){
    $button.removeAttr( disabled ).removeClass( disabled );
}
else{
    $button.attr( disabled ,  disabled ).addClass( disabled );
}

EDIT3 *Found the problem!* It wasn t the regex itself, it was how I passed regex to Regex function... it should be

 new RegExp(/^.+@.+..{2,8}$/);

不适用

new RegExp("^.+@.+..{2,8}$");

正如我说过的那样,这件事对我来说是很新的:

最佳回答

简言之,真正的电子邮件地址的管理机构并不简单,因为它应当符合《海运法》的标准。

然而,如果为你工作,我就没有问题:

// The regex: /^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,4})$/

// To test it:
var regex = new RegExp(/^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,4})$/);

if( regex.test( your_string ) ){ ... } // returns true if valid

如果你真的需要接受“纽约”的标志,你可以使用Birei所贴的reg。

问题回答

Not a Javascript expert, but with your definition of any_symbols@any_symbols.two_to_eight_symbols I think this could work:

^[^@]+@[^.]+..{2,8}$

你们需要逃脱鞭::

 any_symbols@four_symbols .match( ^.+@.+\..{2,8}$ );
                                        ^^

否则, Java的成文如下:.,作为不知名的逃跑顺序和

For characters not listed [...] a preceding backslash is ignored, but this usage is deprecated and should be avoided.

(Damn,这很难找到......)





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签