Go Back   DeveloperBarn Forums > Programming & Scripting > JavaScript Programming

Sponsored Links

Discuss "help with email validation" in the JavaScript Programming forum.

JavaScript Programming - Javascript is a cross-browser client-side scripting language. Discuss Javascript and AJAX related scripts here.


Closed Thread « Previous Thread | Next Thread »
 
LinkBack Thread Tools Display Modes
  #1  
Old August 20th, 2008, 01:29 PM
Rebelle's Avatar
V.I.P/Donor


 
Join Date: Mar 2008
Posts: 254
Thanks: 48
Thanked 1 Time in 1 Post
Rep Power: 1
Rebelle is on a distinguished road
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!!!!
Sponsored Links
  #2  
Old August 20th, 2008, 01:54 PM
mehere's Avatar
Super Sarcasm Mistress


 
Join Date: Mar 2008
Location: Wide Awake In Dreamland
Posts: 143
Thanks: 10
Thanked 27 Times in 25 Posts
Rep Power: 1
mehere will become famous soon enough

Awards Showcase
Microsoft SQL Server Classic ASP 
Total Awards: 2

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:
Regret: It hurts to admit when you make mistakes - but when they're big enough, the pain only lasts a second.

Questions to Ponder:
Could it be that all those trick-or-treaters wearing sheets aren’t going as ghosts but as mattresses?

iif([sarcasm]=true,iif([you have to ask]=true,"didn't work","ha ha ha"),"not sarcasm")
copyright © 2008 sbenj69
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
Moderator


 
Join Date: Mar 2008
Location: Somewhere only we know...
Posts: 395
Thanks: 26
Thanked 32 Times in 32 Posts
Blog Entries: 1
Rep Power: 1
richyrich will become famous soon enough

Awards Showcase
Classic ASP JavaScript 
Total Awards: 2

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
V.I.P/Donor


 
Join Date: Mar 2008
Posts: 254
Thanks: 48
Thanked 1 Time in 1 Post
Rep Power: 1
Rebelle is on a distinguished road
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
Moderator


 
Join Date: Mar 2008
Location: Somewhere only we know...
Posts: 395
Thanks: 26
Thanked 32 Times in 32 Posts
Blog Entries: 1
Rep Power: 1
richyrich will become famous soon enough

Awards Showcase
Classic ASP JavaScript 
Total Awards: 2

Default

Do you mean a JS error or an ASP error?
  #6  
Old August 21st, 2008, 08:50 AM
Rebelle's Avatar
V.I.P/Donor


 
Join Date: Mar 2008
Posts: 254
Thanks: 48
Thanked 1 Time in 1 Post
Rep Power: 1
Rebelle is on a distinguished road
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
Moderator


 
Join Date: Mar 2008
Location: Somewhere only we know...
Posts: 395
Thanks: 26
Thanked 32 Times in 32 Posts
Blog Entries: 1
Rep Power: 1
richyrich will become famous soon enough

Awards Showcase
Classic ASP JavaScript 
Total Awards: 2

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
V.I.P/Donor


 
Join Date: Mar 2008
Posts: 254
Thanks: 48
Thanked 1 Time in 1 Post
Rep Power: 1
Rebelle is on a distinguished road
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
Moderator


 
Join Date: Mar 2008
Location: Somewhere only we know...
Posts: 395
Thanks: 26
Thanked 32 Times in 32 Posts
Blog Entries: 1
Rep Power: 1
richyrich will become famous soon enough

Awards Showcase
Classic ASP JavaScript 
Total Awards: 2

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
V.I.P/Donor


 
Join Date: Mar 2008
Posts: 254
Thanks: 48
Thanked 1 Time in 1 Post
Rep Power: 1
Rebelle is on a distinguished road
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
Forum Jump

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
[ASP.Net/VB.Net] Programmatically Add Item to Validation Summary jmurrayhead Code Samples 0 March 20th, 2008 09:41 AM


All times are GMT -4. The time now is 01:47 PM.



Content Relevant URLs by vBSEO 3.2.0