DeveloperBarn Forums

DeveloperBarn

Programming & IT forum

help with email validation

This is a discussion on help with email validation within the JavaScript Programming forums, part of the Programming & Scripting category; Hi All, I've added a line to make sure the email address is not blank...but I can't get the validation ...

Go Back   DeveloperBarn Forums > Programming & Scripting > JavaScript Programming

  #1  
Old August 20th, 2008, 01:29 PM
Rebelle's Avatar
Barn Loyal
 
Join Date: Mar 2008
Posts: 748
Rep Power: 2
Rebelle will become famous soon enough
Default help with email validation

Hi All,

I've added a line to make sure the email address is not blank...but I can't get the validation part working. if i uncomment the commented lines in red, i get the error: error '8004020e' ...this has to do with validation when i google. am i using that validator the wrong way?

Code:
<script>
function onSubmitForm() {
    var formDOMObj = document.frmSend;

    if (formDOMObj.EqNo.value == "")
	alert("Please enter EQNumber!")
    if (formDOMObj.EqDesc.value == "")
	alert("Please enter EQDescription!")
    if (formDOMObj.EmailAdd.value == "")
	alert("Please enter your email address!")
   // if (emailValidator(formDOMObj.EmailAddress.value,"Please enter a valid email address"))    else
        return true;
    return false;
}

//function emailValidator(){
//	var emailExp = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
//	if(formDOMObj.EmailAdd.match(emailExp)){
//		return true;
//	}
//else{
//		return false;
//	}
//}
</script>
Thanks peeps!!!!
  #2  
Old August 20th, 2008, 01:54 PM
mehere's Avatar
Super Sarcasm Mistress
 
Join Date: Mar 2008
Real name: Joanne
Location: Wide Awake In Dreamland
Posts: 375
Rep Power: 6
mehere is just really nicemehere is just really nicemehere is just really nicemehere is just really nicemehere is just really nice
Default

okay, my javascript is not the greatest. but try something like this
Code:
<script>
function onSubmitForm() {
    var formDOMObj = document.frmSend;
    var emailExp = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;

    if (formDOMObj.EqNo.value == "")
	alert("Please enter EQNumber!")
    if (formDOMObj.EqDesc.value == "")
	alert("Please enter EQDescription!")
    if (formDOMObj.EmailAdd.value == "")
	alert("Please enter your email address!")
    if (!(formDOMObj.EmailAdd.match(emailExp)))
    	alert("Please enter a valid email address")
    return false;
}
</script>

Comments on this post
Rebelle agrees: Thanked Post
__________________
Quote of the Month:
Mistakes: It could be that the purpose of your life is only to serve as a warning to others.

Questions to Ponder:
Why do banks charge you a "non-sufficient funds fee" on money they already know you don't have?

iif([sarcasm]=true,iif([you have to ask]=true,"didn't work","ha ha ha"),"not sarcasm")
copyright © 2008 sbenj69

Sarchasm: The gulf between the author of sarcastic wit and the person who doesn't get it.
The Following User Says Thank You to mehere For This Useful Post:
Rebelle (August 21st, 2008)
  #3  
Old August 20th, 2008, 02:40 PM
richyrich's Avatar
Administrator
 
Join Date: Mar 2008
Real name: Rich
Location: Somewhere only we know...
Posts: 1,347
Blog Entries: 6
Rep Power: 8
richyrich is a name known to allrichyrich is a name known to allrichyrich is a name known to allrichyrich is a name known to allrichyrich is a name known to allrichyrich is a name known to all
Default

You'll need to return true at some point or the form won't submit. Using mehere's example, try this:-
Code:
function onSubmitForm() {
    var formDOMObj = document.frmSend;
    var emailExp = new RegExp("^[\w\-\.\+]+@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$");
 
    if (formDOMObj.EqNo.value == ""){
    alert("Please enter EQNumber!");
    return false;
    }
    if (formDOMObj.EqDesc.value == ""){
    alert("Please enter EQDescription!");
    return false;
    }
    if (!emailExp.test(formDOMObj.EmailAdd.value)){
    alert("Please enter a valid email address");
    return false;
    }
    return true;
}
</script>
You shouldn't need to check for a blank email because it won't pass the regex test.

Hope that helps.

<edit>I couldn't get it to work using \w. You could try this expression "^[A-Za-z0-9\-.+]+@[A-Za-z0-9.-]+[.]+[A-Za-z]{2,4}$" That will only accept email addresses in the format name@domain.ext You may want to accept emails with name@domain in which case change +[.] to +\. </edit>

Comments on this post
mehere agrees: Thanked Post
Rebelle agrees: Thank you!

Last edited by richyrich; August 20th, 2008 at 02:49 PM.
The Following User Says Thank You to richyrich For This Useful Post:
mehere (August 20th, 2008)
  #4  
Old August 21st, 2008, 08:39 AM
Rebelle's Avatar
Barn Loyal
 
Join Date: Mar 2008
Posts: 748
Rep Power: 2
Rebelle will become famous soon enough
Default

Thanks Mehere & RR....

RR, I tried the below but it didn't work, kept getting error 8004020e...and it no longer alerted for the other fields. So, the only way to not get the error was to comment out the lines.

Code:
<script>
function onSubmitForm() {
    var formDOMObj = document.frmSend;
//    var emailExp = new RegExp("^[\w\-\.\+]+@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$");
 
    if (formDOMObj.EqNo.value == ""){
    alert("Please enter EQNumber!");
    return false;
    }
    if (formDOMObj.EqDesc.value == ""){
    alert("Please enter EQDescription!");
    return false;
    }
//    if (!emailExp.test(formDOMObj.EmailAdd.value)){
//    alert("Please enter a valid email address");
//    return false;
//    }
    return true;
}
</script>
So I tried the var line from mehere, and it worked with your if , alert, return... like below:

Code:
<script>
function onSubmitForm() {
    var formDOMObj = document.frmSend;
    var emailExp = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/; 
    if (formDOMObj.EqNo.value == ""){
    alert("Please enter EQNumber!");
    return false;
    }
    if (formDOMObj.EqDesc.value == ""){
    alert("Please enter EQDescription!");
    return false;
    }
    if (!emailExp.test(formDOMObj.EmailAdd.value)){
    alert("Please enter a valid email address");
    return false;
    }    return true;
}
</script>
On this, do you mean they wouldn't have to enter the .com, .net, etc? i think i need the whole thing...(.com) because I am using asp mail (cdo).
Quote:
<edit>I couldn't get it to work using \w. You could try this expression "^[A-Za-z0-9\-.+]+@[A-Za-z0-9.-]+[.]+[A-Za-z]{2,4}$" That will only accept email addresses in the format name@domain.ext You may want to accept emails with name@domain in which case change +[.] to +\. </edit>
  #5  
Old August 21st, 2008, 08:46 AM
richyrich's Avatar
Administrator
 
Join Date: Mar 2008
Real name: Rich
Location: Somewhere only we know...
Posts: 1,347
Blog Entries: 6
Rep Power: 8
richyrich is a name known to allrichyrich is a name known to allrichyrich is a name known to allrichyrich is a name known to allrichyrich is a name known to allrichyrich is a name known to all
Default

Do you mean a JS error or an ASP error?
  #6  
Old August 21st, 2008, 08:50 AM
Rebelle's Avatar
Barn Loyal
 
Join Date: Mar 2008
Posts: 748
Rep Power: 2
Rebelle will become famous soon enough
Default

hmmm...not sure...maybe asp?

error '8004020e'

the line is 222 which is from my cdo code...

this line:

Code:
.Send
  #7  
Old August 21st, 2008, 08:53 AM
richyrich's Avatar
Administrator
 
Join Date: Mar 2008
Real name: Rich
Location: Somewhere only we know...
Posts: 1,347
Blog Entries: 6
Rep Power: 8
richyrich is a name known to allrichyrich is a name known to allrichyrich is a name known to allrichyrich is a name known to allrichyrich is a name known to allrichyrich is a name known to all
Default

That is an error with CDO, normally related to an invalid email address.

When you uncomment the JS code and enter something like 'Rebelle' in the email text box, what happens?
  #8  
Old August 21st, 2008, 09:05 AM
Rebelle's Avatar
Barn Loyal
 
Join Date: Mar 2008
Posts: 748
Rep Power: 2
Rebelle will become famous soon enough
Default

Hi RR,

If i uncomment the line and enter Rebelle...I don't get the error but I also leave the other fields blank so i could get alert but I don't get the alerts.....

it's when i leave the email address field blank that I get the error......but when i leave the other fields blank too, i don't get any alerts....just the error.
  #9  
Old August 21st, 2008, 09:11 AM
richyrich's Avatar
Administrator
 
Join Date: Mar 2008
Real name: Rich
Location: Somewhere only we know...
Posts: 1,347
Blog Entries: 6
Rep Power: 8
richyrich is a name known to allrichyrich is a name known to allrichyrich is a name known to allrichyrich is a name known to allrichyrich is a name known to allrichyrich is a name known to all
Default

The speech marks are missing off this line:-
Code:
var emailExp = "/^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$"; 
<edit>Actually, this line is all wrong. See how mine was:-
Code:
var emailExp = new RegExp("^[\w\-\.\+]+@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$");
We're using regular expressions to test for a match of this pattern. You have to define it as a RegExp pattern</edit>

Last edited by richyrich; August 21st, 2008 at 09:19 AM.
  #10  
Old August 21st, 2008, 09:32 AM
Rebelle's Avatar
Barn Loyal
 
Join Date: Mar 2008
Posts: 748
Rep Power: 2
Rebelle will become famous soon enough
Default

ok...i retried and retried with the original line but same thing kept happening.

so I tried changing the " and " to / and / ... like below:

from:

Code:
var emailExp = new RegExp("^[\w\-\.\+]+@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$");
to:

Code:
var emailExp = new RegExp(/^[\w\-\.\+]+@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/);
will this cause any issues doing it like this?
Closed Thread

  DeveloperBarn Forums > Programming & Scripting > JavaScript Programming

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads

Thread Thread Starter Forum Replies Last Post
validation todd2006 JavaScript Programming 5 July 30th, 2008 09:23 AM
date validation todd2006 JavaScript Programming 3 July 6th, 2008 11:39 AM
textbox validation todd2006 JavaScript Programming 6 July 2nd, 2008 01:30 PM
checkbox validation todd2006 JavaScript Programming 3 June 18th, 2008 07:22 PM
Programmatically Add Item to Validation Summary jmurrayhead .Net Code Samples 0 March 20th, 2008 09:41 AM


All times are GMT -4. The time now is 11:17 PM.


Copyright ©2008-2010, DeveloperBarn

Content Relevant URLs by vBSEO 3.3.2