473,785 Members | 2,468 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Validate EMail with RegExp...

My understanding of regular expressions is rudimentary,
at best.

I have this RegExp to to a very simple validation of an
email-address, but it turns out that it refuses to
accept mail-addresses with hypens in them.

Can anybody please help me adjust it so it will accept
addresses like da*******@test-domain.net too?

Here's what got:

function validateEmail(e Mail) {
return /^(\w+\.)*(\w+)@ (\w+\.)+([a-zA-Z]{2,4})$/.test(eMail);
}

TIA...

--
Dag.

Work is the curse of the drinking classes
-- Oscar Wilde
Nov 3 '05
12 5028
Dag Sunde wrote:
"David Dorward" <do*****@yahoo. com> [...]
Dag Sunde wrote:
I have this RegExp to to a very simple validation of an
email-address, but it turns out that it refuses to
accept mail-addresses with hypens in them.

http://www.ex-parrot.com/~pdw/Mail-RFC822-Address.html


You're kidding me, right?

That is the longest monster of a regular expression I've ever seen!


And probably the most useless one (in this newsgroup), taking into account
that it is based on an obsolete RFC, is highly unlikely to be needed in
forms, and uses features no JS/ECMAScript engine supports.

Nice try, though.
PointedEars
Nov 3 '05 #11
JRS: In article <lV************ *******@juliett .dax.net>, dated Thu, 3
Nov 2005 09:39:29, seen in news:comp.lang. javascript, Dag Sunde
<me@dagsunde.co m> posted :
I have this RegExp to to a very simple validation of an
email-address, but it turns out that it refuses to
accept mail-addresses with hypens in them.

Can anybody please help me adjust it so it will accept
addresses like da*******@test-domain.net too?

Here's what got:

function validateEmail(e Mail) {
return /^(\w+\.)*(\w+)@ (\w+\.)+([a-zA-Z]{2,4})$/.test(eMail);
}


It is pointless to attempt to validate an E-mail address format in
detail, unless you are writing an E-mail system, or unless the task is
purely pedagogical. A RegExp that will accept me@dagsunde.com will also
accept im@dogsendu.com, and for any given message at least one of those
is wrong. There's no way round it; someone typing an E-address just has
to be careful to get it right.

If a test does not accept any RFC-permitted E-address, there will be
complaints; if an allegedly-full test fails to reject any non-permitted
address, there will be complaints. And if the RFCs change the format
range, full-check code will have to be changed too.

Testing with /.+@.+\..+/ or /^.+@.+\..+$/, to ensure at least x@y.z,
does make sense as a check that something like an E-address has been put
in the field, rather than say a price or an arbitrary vulgarity.

See <URL:http://www.merlyn.demo n.co.uk/js-valid.htm#VEmA> .

--
John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4
<URL:http://www.jibbering.c om/faq/> JL/RC: FAQ of news:comp.lang. javascript
<URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Nov 3 '05 #12
"Dr John Stockton" <jr*@merlyn.dem on.co.uk> wrote in message
news:DQ******** ******@merlyn.d emon.co.uk...
JRS: In article <lV************ *******@juliett .dax.net>, dated Thu, 3
Nov 2005 09:39:29, seen in news:comp.lang. javascript, Dag Sunde
<me@dagsunde.co m> posted :
I have this RegExp to to a very simple validation of an
email-address, but it turns out that it refuses to
accept mail-addresses with hypens in them.

Can anybody please help me adjust it so it will accept
addresses like da*******@test-domain.net too?

Here's what got:

function validateEmail(e Mail) {
return /^(\w+\.)*(\w+)@ (\w+\.)+([a-zA-Z]{2,4})$/.test(eMail);
}
It is pointless to attempt to validate an E-mail address format in
detail, unless you are writing an E-mail system, or unless the task is
purely pedagogical. A RegExp that will accept me@dagsunde.com will also
accept im@dogsendu.com, and for any given message at least one of those
is wrong. There's no way round it; someone typing an E-address just has
to be careful to get it right.


Of course.
If a test does not accept any RFC-permitted E-address, there will be
complaints; if an allegedly-full test fails to reject any non-permitted
address, there will be complaints. And if the RFCs change the format
range, full-check code will have to be changed too.

Testing with /.+@.+\..+/ or /^.+@.+\..+$/, to ensure at least x@y.z,
does make sense as a check that something like an E-address has been put
in the field, rather than say a price or an arbitrary vulgarity.

See <URL:http://www.merlyn.demo n.co.uk/js-valid.htm#VEmA> .


That is the only thing i want, to verify that the input at least can
be interpreted as an email-address. My problem was that my former regexp.
balked at 'x' or 'z' with hypens in them.

--
Dag.
Nov 4 '05 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
1969
by: michaaal | last post by:
I realize this is not solely an ASP question, but I thought you guys might know the answer and I couldn't find anywhere else to post this. If you have suggestions for this I am more than happy to post somewhere else. How can I, using ASP code, validate whether or not an email address is a real one? For example, if I have the email address called roy@rogers.net how can I determine if it is real?
10
7694
by: Andrew DeFaria | last post by:
I was reading my O'Reilly JavaScript The Definitive Guide when I came across RegExp and thought I could tighten up my JavaScript code that checks for a valid email address. Why does the following not appear to work: var email_address = "Joe@Schmoe"; var email_regex = new RegExp ("^(\\w+)(\@)(\\w+)(\.)(\\w+)$"); var result = email_regex.exec (email_address); alert (" result = \"" + result + "\"\n" + " result = \"" + result + "\"\n" + "...
13
4810
by: Eddie | last post by:
I need to validate a text input field. I just want to say if user enters 93101 or 93102 or 93103 or 93105 or 93106 or 93107 or 93108 or 93109 or 93110 or 93111 or 93116 or 93117 or 93118 or 93120 or 93121 or 93130 or 93140 or 93150 or 93160 or 93190 or 93199 or 93199 or 93401 or 93402 or 93403 or 93405 or 93406 or 93407 or 93408 or 93409 or 93410 or 93412
3
3381
by: MJ | last post by:
For some reason the following script does not work in Netscape/Mozilla, but works fine in IE and Opera. It is supposed to check the syntax, make sure there is a valid TLD (yes, those are all of the current TLDs), and allow for addresses with or without trailing slashes or page addresses. Anybody have any ideas on how to get this to work in Netscape? I suspect it has something to do with the regular expression, but I can't get it to...
24
7131
by: Arno R | last post by:
Hi all, I have a client with several shoe-shops. Customers can leave their email-address if they want to be notified when there is a sale. Input is validated with instr() I am checking for @ and . (required) and also checking for spaces (not allowed). But: A LOT (5-10%) of the addresses still are wrong; (provider doesn't exist) or email-address not valid (anymore). When sending bulk-mail its a nasty problem to get the false addresses...
3
2026
by: c676228 | last post by:
Hi everyone, I just realized that it's so important to validate each string, I mean 'each' before you insert data from asp page into database. I guess some customers just copy data from some electronic document and paste into form field which it will probably mess up the program. for example, we have a customer who wants to enter AH12345 into one of our fields, it appears АН12345 in hidden field of our asp page, but it displayed...
23
2880
by: codefire | last post by:
Hi, I am trying to get a regexp to validate email addresses but can't get it quite right. The problem is I can't quite find the regexp to deal with ignoring the case james..kirk@fred.com, which is not valid. Here's my attempt, neither of my regexps work quite how I want: import os import re
11
8195
by: TokyoJ | last post by:
I run a small camp in Alaska for kids and my director is asking for a web form. Could someone please have a look and offer some advice on where I'm making mistake(s)? I'm using the RegExp function to validate 3 types of fields: text, radio button, dropdown menu. but the code doesn't validate. After 2 days, it's time I asked for guidence. Criteria: Text: only alphabet, no numerals, allowed Radio: one must be selected, Dropdown: an option...
3
7232
by: samuelberthelot | last post by:
Hello, I would like to validate a date in a textbox on the onChange event. The date must be in the format 01/01/2007 I would like to use a regular expression to validate it but I'm not very familiar with them. How can I do it ? Thanks
0
9647
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
9491
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10163
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10104
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9959
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8988
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
4063
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3668
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.