There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
 
Tag Cloud
access audio avg avg 8 bios blue screen boot bsod computer connection cpu crash css dell desktop dma driver drivers dvd email error excel explorer firefox firefox 3 freeze gimp graphics hard drive hardware hijackthis hjt install internet internet explorer itunes keyboard laptop macro malware monitor motherboard network networking outlook outlook 2003 outlook 2007 outlook express pio problem problems router seo server slow sound sp3 spyware trojan usb video virtumonde virus vista vundo windows windows vista windows xp winxp wireless
Software Development
Search
Search in:
 
Advanced Search
Tech Support Guy Forums > Software & Hardware > Software Development >
javascript comparison


HELLO AND WELCOME! Before you can post your question, you'll have to register -- it's completely free! Click here to join today! We highly recommend that you print a copy of our Guide for New Members. Enjoy!

 
Thread Tools
SabreWolf3's Avatar
Senior Member with 227 posts.
 
Join Date: Nov 2000
Location: Washington, DC
18-Jul-2002, 01:55 PM #1
javascript comparison
Hi all,

I use a popular script for client-side validation of a form. The following snippet checks to make sure my email field, named "email" (a) is not blank, (b) contains at least one "@" sign and (c) contains at least one "."

I also need it to verify that there is not more than one "@" sign as well, because I have received a lot of form submissions lately with people typing two addresses, like abcd@home.com OR abcd@work.com. This screws up my asp mailer because the asp checks for more than one "@" sign, and if it finds more than one, it just doesn't send a copy to whoever submitted the form. I need to change this behavior and have the page throw an error if they try to enter more than one email address.

Can anyone help? Thanks !!!!

Code:
//Check for an e-mail address and that it is valid

if ((document.comments.email.value == "") ||

(document.comments.email.value.length > 0 

&& (document.comments.email.value.indexOf("@",0) == - 1 ||

document.comments.email.value.indexOf(".",0) == - 1))) { 

errorMsg += "\n\tE-mail Address \t- Enter your valid e-mail address please";
}
Rockn's Avatar
Computer Specs
Distinguished Member with 17,888 posts.
 
Join Date: Jul 2001
Location: Mexico of the North, MN
Experience: Disenfranchised American Male
18-Jul-2002, 02:20 PM #2
Here's an ASP server side script from 4Guys:

http://www.4guysfromrolla.com/webtech/051999-1.shtml
SabreWolf3's Avatar
Senior Member with 227 posts.
 
Join Date: Nov 2000
Location: Washington, DC
18-Jul-2002, 03:39 PM #3
Hey Rockn,

That looks like a cool script, but there's only one problem. The form I'm validating is 6 pages long, and I need to do it client-side so that there's no chance my visitors can lose the information they've typed in the fields.

I really need to find out how to count the instances of the "@" symbol in the email field.

Thanks,
Richard
Rockn's Avatar
Computer Specs
Distinguished Member with 17,888 posts.
 
Join Date: Jul 2001
Location: Mexico of the North, MN
Experience: Disenfranchised American Male
18-Jul-2002, 03:51 PM #4
They won't loose any of their information if you place the code in the same page and it doesn't get refreshed. Put validation on each entry point on the form before it will process the next section. To find the number of @ symbols you will need to do a count of the instances in the string and if they are greater than 1 send them an error message. You might want to do pop up warnings, that way it wil definately not refresh the form and loose the information on it.
__________________
**Disclaimer** Anything below this line ^ is part of my signature for those that may be confused

Sadly, there are no integers on this scale, so your gangly adolescent attempt to be clever has proved futile....Dieter

I have the right "NOT" to be tolerant of others because they are different, weird, or tick me off.....Parody of Andy Rooney

There are no stupid questions, but there are a LOT of inquisitive idiots.
SabreWolf3's Avatar
Senior Member with 227 posts.
 
Join Date: Nov 2000
Location: Washington, DC
18-Jul-2002, 04:33 PM #5
got it!!
Hi Rockn, after toiling away, I finally managed to figure it out. For anyone interested, here's the code I ended up using...



Code:
[script language="JavaScript"][!--
function countInstances(string,word) {
var count = 0;
var substrings = string.split(word);
return substrings.length - 1;
}
//--][/script]

//Check for more than one "@" sign in the email field
var count = countInstances(document.comments.email.value,"@");
if (count >1){
errorMsg += "\n\tE-mail Address \t- There can only be one @ sign in an e-mail address"
errorMsg +="\n \t\t\t (please enter one e-mail address only)";
}
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

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 Off
Refbacks are Off

You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -4. The time now is 01:14 AM.
Copyright © 1996 - 2008 TechGuy, Inc. All rights reserved.
Powered by vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Powered by Cermak Technologies, Inc.