Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 08-17-2004, 02:30 AM   PM User | #1
jinntet
New to the CF scene

 
Join Date: Aug 2004
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
jinntet is an unknown quantity at this point
Help validating forms

Hey,

I really, really need some help with form validation. I have tried over 20 codes to get an email address to validate but none of them work. I'm not very good with Javascript! As far as I can see I am placing the function in the correct place and also calling it at the right time. I would be greatful for any help any one could give me.

Thanks
jinntet is offline   Reply With Quote
Old 08-17-2004, 03:08 AM   PM User | #2
Willy Duitt
Banned

 
Join Date: Sep 2003
Posts: 3,620
Thanks: 0
Thanked 0 Times in 0 Posts
Willy Duitt is an unknown quantity at this point
How can we possibly help you when you have failed to provide any codes and/or the email validation requirements.....

.....Willy
Willy Duitt is offline   Reply With Quote
Old 08-17-2004, 09:47 AM   PM User | #3
Kor
Red Devil Mod


 
Kor's Avatar
 
Join Date: Apr 2003
Location: Bucharest, ROMANIA
Posts: 8,478
Thanks: 58
Thanked 379 Times in 375 Posts
Kor has a spectacular aura aboutKor has a spectacular aura about
You may try this very very simple email validator:

PHP Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<
html>
<
head>
<
title>Untitled Document</title>
<
meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<
script language="JavaScript" type="text/JavaScript">
function 
validate(f){
var 
p=0;
var 
= new Array()
v=f.email.value.split('');
for (var 
i=0;i<v.length;i++){
if((
v[i]=='@')||(v[i]=='.')){
p++
}
}
if(
p<2){
alert('Please type a valide e-mail');
f.email.focus();
return 
false
}
}
</script>
</head>
<body>
<form onsubmit="return validate(this)">
e-mail <input name="email" type="text">
<br>
<input type="submit" value="Submit">
</form>
</body>
</html> 
Well, it is not a complete validator (the email validator is a really complex problem), but it covers all the possibilities but only according to the following presumtions:

1. the minimum mail string type is something like 'name@domain.extension' (it have at least one "@" and one "." characters)
2. the most common user's mistakes are
- omitting the input
- omitting the extension (that means the dot)
- omitting the @
3. users are well intentioned people and their omitions are accidentally ones, by pure mistake
__________________
KOR
Offshore programming
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
Kor is offline   Reply With Quote
Old 08-17-2004, 10:02 AM   PM User | #4
jinntet
New to the CF scene

 
Join Date: Aug 2004
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
jinntet is an unknown quantity at this point
Sorry, was a bit tired been at it for over 10 hours. Told you I was not very good at it. Its a shopping cart so the form is for customers to order their good. So they submit all their details like name, address, credit card details, email and at the end their is a submit button to submit the order. The order is submitting fine but I can't get anything to validate like poscode, credit card details and email address. I would like a pop up alert telling them if anything is incorrect. I have just tried the code but it does not work.

<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script language="JavaScript" type="text/JavaScript">
function validate(f){
var p=0;
var v = new Array()
v=f.email.value.split('');
for (var i=0;i<v.length;i++){
if((v[i]=='@')||(v[i]=='.')){
p++
}
}
if(p<2){
alert('Please type a valide e-mail');
f.email.focus();
return false
}
}
</script>
</head>
<body>
<form onsubmit="return validate(this)">
e-mail <input name="email" type="text">
<br>
<input type="submit" value="Submit">
</form>
</body>
</html>
jinntet is offline   Reply With Quote
Old 08-17-2004, 10:05 AM   PM User | #5
jinntet
New to the CF scene

 
Join Date: Aug 2004
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
jinntet is an unknown quantity at this point
Thanks for this but I'm afraid it did not work for me.
jinntet is offline   Reply With Quote
Old 08-17-2004, 10:25 AM   PM User | #6
Kor
Red Devil Mod


 
Kor's Avatar
 
Join Date: Apr 2003
Location: Bucharest, ROMANIA
Posts: 8,478
Thanks: 58
Thanked 379 Times in 375 Posts
Kor has a spectacular aura aboutKor has a spectacular aura about
Quote:
Thanks for this but I'm afraid it did not work for me.
I beg your pardon??? It should work on every browsers I know. At least it works for what I have now installed (NS6, NS7, Mozilla 1.7, Firefox, Opera 5) on XP on PC ...

It validates the classic way. If there is no email or if the string has not a '@' and a '.', an alert message apperas and the submit is stopped. If not, the data is submitted.

What in fact don't work for you???
__________________
KOR
Offshore programming
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
Kor is offline   Reply With Quote
Old 08-17-2004, 10:35 AM   PM User | #7
jinntet
New to the CF scene

 
Join Date: Aug 2004
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
jinntet is an unknown quantity at this point
I'm using netscape 7. There was no alert telling me the email address was not valid when I entered just letters.
jinntet is offline   Reply With Quote
Old 08-17-2004, 10:41 AM   PM User | #8
Kor
Red Devil Mod


 
Kor's Avatar
 
Join Date: Apr 2003
Location: Bucharest, ROMANIA
Posts: 8,478
Thanks: 58
Thanked 379 Times in 375 Posts
Kor has a spectacular aura aboutKor has a spectacular aura about
Should work either. check whether you have javascript enable or not,

Edit-Preferences-Advanced-Scripts&Plugins

and make sure to check the small checkbox Enable javaScript for "Navigator"
__________________
KOR
Offshore programming
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
Kor is offline   Reply With Quote
Old 08-17-2004, 10:54 AM   PM User | #9
jinntet
New to the CF scene

 
Join Date: Aug 2004
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
jinntet is an unknown quantity at this point
Both of these are checked but there is still no alert message advising that an invalid email address has been entered.
jinntet is offline   Reply With Quote
Old 08-17-2004, 11:09 AM   PM User | #10
Kor
Red Devil Mod


 
Kor's Avatar
 
Join Date: Apr 2003
Location: Bucharest, ROMANIA
Posts: 8,478
Thanks: 58
Thanked 379 Times in 375 Posts
Kor has a spectacular aura aboutKor has a spectacular aura about
it must work when you press the submit button. Isn't that what you wanted?

If you don't see the alert, there must be something wrong with your browser or your OS..

Can you see the alert on this?
PHP Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<
html>
<
head>
<
title>Untitled Document</title>
<
meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</
head>
<
body onload="alert('see this?')">
</
body>
</
html
By the way, make sure your code has the doctype and the content-type.
__________________
KOR
Offshore programming
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
Kor is offline   Reply With Quote
Old 08-17-2004, 02:00 PM   PM User | #11
jinntet
New to the CF scene

 
Join Date: Aug 2004
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
jinntet is an unknown quantity at this point
Yes I want the alert when you press the submit button. I can see the see this one you gave me but still nothing with the first piece of code. I have this in at the top
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML>
<link rel=stylesheet type="text/css" href="JLstyle.css">
<BODY>
<IMG SRC="crakinlogo.gif" width=100% height=150><BR>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script language="JavaScript" type="text/JavaScript">
function validate(f){
var p=0;
var v = new Array()
v=f.email.value.split('');
for (var i=0;i<v.length;i++){
if((v[i]=='@')||(v[i]=='.')){
p++
}
}
if(p<2){
alert('Please type a valide e-mail');
f.email.focus();
return false
}
}

Then in my form I have this
<TD><B class="first">Email Address:</B></TD>
<TD><INPUT TYPE="EMAIL" NAME="email" SIZE=30 onsubmit="return validate(this)"></TD>
jinntet is offline   Reply With Quote
Old 08-17-2004, 04:06 PM   PM User | #12
Kor
Red Devil Mod


 
Kor's Avatar
 
Join Date: Apr 2003
Location: Bucharest, ROMANIA
Posts: 8,478
Thanks: 58
Thanked 379 Times in 375 Posts
Kor has a spectacular aura aboutKor has a spectacular aura about
Quote:
TD><B class="first">Email Address:</B></TD>
<TD><INPUT TYPE="EMAIL" NAME="email" SIZE=30 onsubmit="return validate(this)"></TD>
onsumit event handler is to be used only on [b]FORM[/] tag. not on on input. I have gave you an example which you have modified, thatis why you got no proper answer. Review my example, please.
__________________
KOR
Offshore programming
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
Kor is offline   Reply With Quote
Old 08-17-2004, 07:09 PM   PM User | #13
jinntet
New to the CF scene

 
Join Date: Aug 2004
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
jinntet is an unknown quantity at this point
I'm sorry but I don't know what you mean. This is my first time doing Javascript and I don't really understand it. Do you mean that my form can't be in a table if I want to validate it?
jinntet is offline   Reply With Quote
Old 08-17-2004, 07:59 PM   PM User | #14
mcdougals4all
Regular Coder

 
Join Date: Jul 2004
Location: mile high city
Posts: 482
Thanks: 0
Thanked 0 Times in 0 Posts
mcdougals4all is an unknown quantity at this point
The table is not the problem. You are referencing the validation function from the wrong point in your html - on the input tag within the form rather than form tag itself.

Your opening form tag should look like something like this...

Code:
<form onsubmit="return validate(this)"...
Applying onsubmit to a single input has no effect because you are actually submitting the form which then collects all the data from your inputs.
mcdougals4all is offline   Reply With Quote
Old 08-17-2004, 09:19 PM   PM User | #15
jinntet
New to the CF scene

 
Join Date: Aug 2004
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
jinntet is an unknown quantity at this point
Do you mean somewhere here <FORM NAME="form1" METHOD="POST" ?
jinntet is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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

Forum Jump


All times are GMT +1. The time now is 02:50 AM.


Advertisement
Log in to turn off these ads.