SitePoint Sponsor |
|
User Tag List
Results 1 to 8 of 8
Thread: email validation
-
Mar 17, 2007, 18:27 #1
- Join Date
- Mar 2007
- Posts
- 12
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
email validation
I have searched through the forums but can't find quite what I need. I have a form that needs to verify the email address with specific requirements... the email field should have only one '@' in it and may contain letters, numbers, and the following characters: hyphen, underscore and period.
Also, the first character must be a letter. If this field fails these validations, it will display an alert box informing the user of the specific error and place the focus on the Email Id field with the text selected.
I have figured out how to make sure that the field isn't empty, but can't figure out the rest... any suggestion or help would be greatly appreciated
Code:function validEmail() { if (document.myForm.emailId.value == "") { alert("Please enter a valid email") return false; } return true; }
-
Mar 17, 2007, 21:27 #2
- Join Date
- Jul 2005
- Location
- West Springfield, Massachusetts
- Posts
- 17,290
- Mentioned
- 198 Post(s)
- Tagged
- 3 Thread(s)
regex
It does help to know what words to use when searching. The word you want to use here is "regex". Search for - email address regex - and you'll get enough results to make your head spin.
Big Change Coming Soon - if you want your PMs save them now!
What you need to do to prepare for our migration to Discourse
A New SitePoint Forum Experience: Our Move to Discourse
-
Mar 18, 2007, 17:48 #3
- Join Date
- Mar 2007
- Posts
- 12
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
wow, thanks... there is a ton of info on regex... a little overwhelming for a newbie, but I've been reading up on it all day and I think I get it a little better now... but my script still isn't working right...
Code:// Email Requirements function var at = ("\@"); var firstLetter = /^([a-zA-Z_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/; var emailId = ("document.myForm.emailId.value"); function validEmail() { with (emailId) { if (document.myForm.emailId.value == "") { alert("Please enter a valid email") myForm.emailId.focus(); return false; } if (at < 1 || at > 1) { alert("You have the wrong amount of @ symbols, Please enter a valid email"); myForm.emailId.focus(); return false; } if (firstLetter.test()) { return addArray(); } else { alert("Your email must begin with a letter, Please enter a valid email"); myForm.emailId.focus(); return false; } } return addArray(); }
Also, I need to have the email field selected after the alert appears when there is an error. I tried myForm.emailId.focus.select(); but it gave me errors.
-
Mar 18, 2007, 19:25 #4
- Join Date
- Jul 2005
- Location
- West Springfield, Massachusetts
- Posts
- 17,290
- Mentioned
- 198 Post(s)
- Tagged
- 3 Thread(s)
email regex
I agree that regex can be a bit overwhelming for a newbie. Believe me, it can be a bit overwhelming for the not-so-newbie too! What with all the different "flavors" that are similar, but different, all the various ways to get the same result, and how other times one little change makes a world of difference, even developers that are gurus in other areas can have trouble with regex. Especially confusing to me are the "short-cuts" eg.
/[a-zA-Z]/ is equivalent to /[a-z]/i
/[a-zA-Z_]/ is equivalent to /[\w]/
Anyway, once you get a good regex, testing for empty '' and multiple ats "@" won't be neccessary, the regex will take care of those. Then you'll only need
Code:// if email OK { //do stuff } else { // alert and return focus }
I don't know, but the focus error might be able to be fixed by using something like
Code:var email_input = document.getElementById('emailId'); email_input.focus();
Last edited by Mittineague; Mar 18, 2007 at 20:02.
Big Change Coming Soon - if you want your PMs save them now!
What you need to do to prepare for our migration to Discourse
A New SitePoint Forum Experience: Our Move to Discourse
-
Mar 19, 2007, 19:41 #5
- Join Date
- Mar 2007
- Posts
- 12
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
thanks for the help! I think I'm going to stay away from the shortcuts, at least until I understand the full codes anyway
and I tried the focus code and it works! thank you so much!
-
Mar 20, 2007, 01:25 #6
- Join Date
- Aug 2006
- Location
- Samsara
- Posts
- 451
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Are there any good guidelines for email validation? Whenever I try to search, I just get an avalanche of peoples' own custom-made functions or tutorials on regexes. Regex I can do fine, but is there any definitive standard for what an email address can or cannot look like?
Cheers,
D.
-
Mar 20, 2007, 09:53 #7
- Join Date
- Jul 2005
- Location
- West Springfield, Massachusetts
- Posts
- 17,290
- Mentioned
- 198 Post(s)
- Tagged
- 3 Thread(s)
standard
There's standards, and there's email client support. AFAIK the current standard is RFC 2822. For the technical read -> http://tools.ietf.org/html/rfc2822
the wiki says
this_part_can_have_up_to_64_chars@and_this_part_up_to_255_chars
but I have never seen one that long.
The part after the @ should be a domain, so I guess what ever rules apply for valid domains also apply to the email address. ie. alphanums and "-" and "."
The part before the @, the "local" part, as per the wiki
- Uppercase and lowercase letters (case sensitive)
- The digits 0 through 9
- The characters ! # $ % & ' * + - / = ? ^ _ ` { | } ~
- The character . provided that it is not the first or last character in the local-part.
Big Change Coming Soon - if you want your PMs save them now!
What you need to do to prepare for our migration to Discourse
A New SitePoint Forum Experience: Our Move to Discourse
-
Mar 20, 2007, 17:55 #8
- Join Date
- Aug 2006
- Location
- Samsara
- Posts
- 451
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
The Hotmail convention seems considerably saner than allowing nearly everything in the printable ASCII range. I can't honestly say that in all my years on the internet that I've ever seen an email address consisting of any of those characters (! # $ % & ' * + - / = ? ^ _ ` { | } ~), except the dash and underscore.
Cheers,
D.
Bookmarks