473,322 Members | 1,408 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Validating an email address

15
Hi

Again its probably easy, but here I am stuck on checking for these in an input.

Can it be checked like this? Do I need to look at some other validation method for this?

Expand|Select|Wrap|Line Numbers
  1.  
  2.     if(( ! document.fmOrder.cdEmail.value == 0 ) && (does not contain an @ and one . in sequence))
  3.     {
  4.         errors.push("Please enter a valid email address");
  5.     }
  6.  
  7.  
  8.  
  9.     if( errors.length > 0 )
  10.     {
  11.         alert(errors.join('\n'));
  12.         return false;
  13.     }
  14.  
  15.     return true;
  16. }
  17.  
Many thanks

M
Aug 22 '07 #1
5 1276
r035198x
13,262 8TB
Hi

Again its probably easy, but here I am stuck on checking for these in an input.

Can it be checked like this? Do I need to look at some other validation method for this?

Expand|Select|Wrap|Line Numbers
  1.  
  2.     if(( ! document.fmOrder.cdEmail.value == 0 ) && (does not contain an @ and one . in sequence))
  3.     {
  4.         errors.push("Please enter a valid email address");
  5.     }
  6.  
  7.  
  8.  
  9.     if( errors.length > 0 )
  10.     {
  11.         alert(errors.join('\n'));
  12.         return false;
  13.     }
  14.  
  15.     return true;
  16. }
  17.  
Many thanks

M
You should use a regular expression for that. Google "email validation in Javascript".
Aug 22 '07 #2
tj111
12
Use regular expressions for checking user input. They seem complicated at first, but once you understand the methods behind it, it makes alot of sense. If your new to regular expressions, give this (Regular Expressions Tutorial) a read, it really helps you understand them. Here's my email address verification script.

Expand|Select|Wrap|Line Numbers
  1. function isEmail (email) {
  2.     var re =/^[A-Za-z0-9]+[\w.-]*?[A-Za-z0-9]+@[A-Za-z0-9]+[\w.-]*?\.[A-Za-z0-9]{2,5}$/;
  3.  
  4.     if (email.match(re)) {
  5.         return true;
  6.     }
  7.     return false;
  8. }
  9.  
Then to check an email you call it like:
Expand|Select|Wrap|Line Numbers
  1. if (!isEmail(form.email.value)) {
  2.   //Things to do if it doesn't match
  3. }
  4. //things to do if it matches
  5.  

Essentially it checks it like this:
1. Starts with any letter or number (1 or more times)
2. Can be followed by any letter, number, or the symbols "_", "-", or "."
3. Contains any letter number 1 or more times immediately before the "@"
4. Has "@" symbol
5. Checks the same as 1-3 above again after the "@" symbol
6. Ends with a "." followed by 2-5 letters or numbers (.com, .co.uk, etc)

I didn't want email address to be able to start or end with "_" or "." because that is common among spammers, but very rare among standard email users.
Aug 22 '07 #3
MKO
15
thanks both,

I read up on it as suggested and its now working. Thanks heaps.

tj - thanks for the logic behind the expression, it really helped me understand how it is working.

Cheers again
Aug 22 '07 #4
pbmods
5,821 Expert 4TB
Changed thread title to better describe the problem.
Aug 22 '07 #5
tj111
12
Yeah the logic makes it so much easier to write and understand regular expressions. Before I read that guide I knew how to write RegExp functions and such, but spent alot of time trying to figure out why certain things got through that shouldn't of and vice versa (and restraining myself from smashing my computer). Once you understand the methodology behind it, regular expressions become easy and very useful.
Aug 22 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Mark | last post by:
Hi, Im trying to validate a form, all the validating works apart from one field. This particular field must consist of the first 2 characters as letters, & the following 5 as numbers. And if it...
17
by: Sue | last post by:
<html> Is there someone here that can help me validate the period as the fourth from the last character in an email address. There is other information and validation on the form I have to do but...
3
by: Guju | last post by:
Hi all, I am a newbie to sql and I need your help. I want to update column (email) from one table to another validating the Customerid column in both table. Update the email address in...
15
by: qazmlp | last post by:
What is the best & fastest way of validating an IPv4 address? Basically, the input can be either in IPAddressv4 or IPAddressv4:port format. Currently I have the following code to validate the...
1
by: Henrik Nyberg | last post by:
Here's a small method for validating email in C#. It may save you some time.. public static bool IsValidEmailAddress(string sEmail) { if (sEmail == null) { return false; } int nFirstAT =...
2
by: Doug | last post by:
I'm a little confused by this functionality. It doesn't seem to be behaving like it should. I am using the following regular expression to validate email addresses:...
6
by: yochessyo | last post by:
Hi, I would like to validate email addresses. I am not interested to validate it with a regex expression but from the email server where the addresses are. I would like to query this server and...
3
by: Tom Anderson | last post by:
Hi all, A hoary old chestnut this - any advice on how to syntactically validate an email address? I'd like to support both the display-name-and-angle-bracket and bare-address forms, and to allow...
1
by: zaidalin79 | last post by:
I am in a JavaScript class, and we have to get all of our code to validate at the w3c website... Here is my code, it does what I want it to do which is require the user to enter the name and either...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.