Go Back  HTML Forums - Free Webmaster Forums and Help Forums > WEBSITE DEVELOPMENT > Client Side Scripting
User Name:
Password:
 

Reply
Thread Tools   Display Modes
  View First Unread
 
Old 09-25-2005, 06:49 AM
  #1
jp_sympa
Aspirant (Level 2)
 
Join Date: Sep 2005
Posts: 12
iTrader: (0)
jp_sympa is an unknown quantity at this point
What is wrong with my onsubmit?

Hi, I have a form with a javascript function to validate my fields but it seems to not work, is there something wrong with my code...

<html>
<head>
<title>Skyway.com.au</title>
<LINK REL=STYLESHEET HREF="../styles/styles.css" TYPE="text/css">

<script type="text/javascript">
<!--

function fieldsOK()
{
var message="";
var id=0;

if (document.form.surname.value.length == 0)
{
message = "* Surname field is empty.\n";
id++;
}

if (document.form.name.value.length == 0)
{
message += "* Name field is empty.\n";
id++;
}

if (document.form.street.value.length == 0)
{
message += "* Street adress field is empty.\n";
id++;
}

if (document.form.suburb.value.length == 0)
{
message += "* Suburb field is empty.\n";
id++;
}

if (document.form.post.value.length == 0)
{
message += "* Post code field is empty.\n";
id++;
}

if (document.form.city.value.length == 0)
{
message += "* City field is empty.\n";
id++;
}

if (document.form.country.value.length == 0)
{
message += "* Country field is empty.\n";
id++;
}

if (document.form.ccn.value.length == 0)
{
message += "* Credit card number field is empty.\n";
id++;
}

if (document.form.card_day.value.length == 0)
{
message += "* Credit card expiry date field is empty.\n";
id++;
}

if (document.form.card_month.value.length == 0)
{
message += "* Credit card expiry month field is empty.\n";
id++;
}

if (document.form.card_year.value.length == 0)
{
message += "* Credit card expiry year field is empty.\n";
id++;
}

if (document.form.email.value.length == 0)
{
message += "* Email field is empty.\n";
id++;
}

if (document.form.post.value.length != 0)
{
for(i=0;i< document.form.post.value.length;i++)
{ // le caractuel est stocké dans c.
char = document.form.post.value.charAt(i);
if ((char < "0") || (char > "9"))
{
message +="* Invalid Post code \n";
id++;
i=document.form.post.value.length;
}
}
}

if (document.form.ccn.value.length != 0)
{
for(i=0;i< document.form.ccn.value.length;i++)
{ // le caractuel est stocké dans c.
char = document.form.ccn.value.charAt(i);
if ((char < "0") || (char > "9"))
{
message +="* Invalid Credit card number \n";
id++;
i=document.form.ccn.value.length;
}
}
}

if (document.form.card_day.value.length != 0)
{
for(i=0;i< document.form.card_day.value.length;i++)
{ // le caractuel est stocké dans c.
char = document.form.card_day.value.charAt(i);
if ((char < "0") || (char > "9"))
{
message +="* Invalid Credit card expiry day \n";
id++;
i=document.form.card_day.value.length;
}
}
}

if (document.form.card_month.value.length != 0)
{
for(i=0;i< document.form.card_month.value.length;i++)
{ // le caractuel est stocké dans c.
char = document.form.card_month.value.charAt(i);
if ((char < "0") || (char > "9"))
{
message +="* Invalid Credit card expiry month \n";
id++;
i=document.form.card_month.value.length;
}
}
}

if (document.form.card_year.value.length != 0)
{
for(i=0;i< document.form.card_year.value.length;i++)
{ // le caractuel est stocké dans c.
char = document.form.card_year.value.charAt(i);
if ((char < "0") || (char > "9"))
{
message +="* Invalid Credit card expiry year \n";
id++;
i=document.form.card_year.value.length;
}
}
}

if (document.form.email.value.length != 0)
{
if ((document.form.email.value.indexOf("@")<0)||(document.form.email.value.indexOf(".")<0))
{
message += "* Invalid email address \n";
id++;
}
}


if (id!=0)
{
message+=" Thank you.";
alert(message);
message="";
id=0;
return false;
}
else
{
return true;
}

}

//-->
</script>
</head>


------------------------------------------------------------------------------------------------------


<form method="POST" onSubmit="return fieldsOK()" action="mail.php">
<table border="1" cellspacing="0" cellpadding="2" bordercolor="black">
<tr>
<td align="right" bgcolor="#FFFF99"><label><b>First Name</b></label></td>
<td align="right" bgcolor="6699CC"><input type="text" name="surname" size="20"></td></tr>
<tr>
<td align="right" bgcolor="#FFFF99"><label><b>Family Name</b></label></td>
<td align="right" bgcolor="#6699CC"><input type="text" name="name" size="20"></td></tr>
<tr>
<td align="right" bgcolor="#FFFF99"><label><b>Street Adress</b></label></td>
<td align="right" bgcolor="6699CC"><input type="text" name="address" size="20"></td></tr>
<tr>
<td align="right" bgcolor="#FFFF99"><label><b>Suburb</b></label></td>
<td align="right" bgcolor="6699CC"><input type="text" name="suburb" size="20"></td></tr>
<tr>
<td align="right" bgcolor="#FFFF99"><label><b>Postcode</b></label></td>
<td align="right" bgcolor="6699CC"><input type="text" name="post" size="5" maxlength="5"></td></tr>
<tr>
<td align="right" bgcolor="#FFFF99"><label><b>City</b></label></td>
<td align="right" bgcolor="6699CC"><input type="text" name="city" size="20"></td></tr>
<tr>
<td align="right" bgcolor="#FFFF99"><label><b>Country</b></label></td>
<td align="right" bgcolor="6699CC"><input type="text" name="country" size="20"></td></tr>
<tr>
<td align="left" bgcolor="#FFFF99"><label><b>Credit Card Number</b></label></td>
<td align="right" bgcolor="#CC0000"><input type="text" name="ccn" size="16" maxlength="16"></td></tr>
<tr>
<td align="left" bgcolor="#FFFF99"><label><b>Expiration Date</b> <sup>(dd/mm/yyy)</sup></label></td>
<td align="right" bgcolor="#CC0000">
<input type="text" name="card_day" size="1" maxlength="2">
<input type="text" name="card_month" size="1" maxlength="2">
<input type="text" name="card_year" size="4" maxlength="4"></td></tr>
<tr>
<td align="left" bgcolor="#FFFF99"><label><b>Email Adress</b></label></td>
<td align="right" bgcolor="#CC0000"><input type="text" name="email" size="20"></td></tr>
</table>
<br>
<input type="submit" value="Complete">
</form>



Thanks.
jp_sympa is offline   Add to del.icio.us Add to del.icio.us    Can you digg it?Can you digg it? Reply With Quote
Old 09-25-2005, 08:03 AM
  #2
justValerie
Aspirant (Level 2)
 
Join Date: Sep 2005
Posts: 16
iTrader: (0)
justValerie is an unknown quantity at this point
Try...

Giving your form a name of "form". That should do it..if not, let us know!

www.justvalerie.com
justValerie is offline   Add to del.icio.us Add to del.icio.us    Can you digg it?Can you digg it? Reply With Quote
Old 09-25-2005, 08:25 AM
  #3
jp_sympa
Aspirant (Level 2)
 
Join Date: Sep 2005
Posts: 12
iTrader: (0)
jp_sympa is an unknown quantity at this point
Sorry, still doesn't work....
jp_sympa is offline   Add to del.icio.us Add to del.icio.us    Can you digg it?Can you digg it? Reply With Quote
Old 09-25-2005, 09:34 AM
  #4
birdbrain
Paladin (Level 15)
 
Join Date: Sep 2005
Location: I have a bijou perch
Posts: 314
iTrader: (0)
birdbrain is an unknown quantity at this point
Hi jp_sympa,

try this..
Code:
<script type="text/javascript">
<!--
function fieldsOK() {
  var message="";
  var id=0;
if (document.forms[0][0].value.length == 0) {
 message = "* Surname field is empty.\n";
id++;
}
if (document.forms[0][1].value.length== 0) {
message += "* Name field is empty.\n";
id++;
}
if (document.forms[0][2].value.length== 0) {
message += "* Street adress field is empty.\n";
id++;
}
if (document.forms[0][3].value.length== 0) {
message += "* Suburb field is empty.\n";
id++;
}
if (document.forms[0][4].value.length== 0) {
message += "* Post code field is empty.\n";
id++;
}
if (document.forms[0][5].value.length== 0) {
message += "* City field is empty.\n";
id++;
}
if (document.forms[0][6].value.length== 0) {
message += "* Country field is empty.\n";
id++;
}
if (document.forms[0][7].value.length== 0) {
message += "* Credit card number field is empty.\n";
id++;
}
if (document.forms[0][8].value.length== 0) {
message += "* Credit card expiry date field is empty.\n";
id++;
}
if (document.forms[0][9].value.length== 0) {
message += "* Credit card expiry month field is empty.\n";
id++;
}
if (document.forms[0][10].value.length== 0) {
message += "* Credit card expiry year field is empty.\n";
id++;
}
if (document.forms[0][11].value.length== 0) {
message += "* Email field is empty.\n";
id++;
}
if (document.forms[0][4].value.length!= 0) {
for(i=0;i< document.forms[0][4].value.length== 0;i++) { // le caractuel est stocké dans c.
charz = document.forms[0][4].value.charAt(i);
if ((charz < "0") || (charz > "9")) {
message +="* Invalid Post code \n";
id++;
i=document.forms[0][4].value.length;
}}}
if (document.forms[0][7].value.length != 0) {
for(i=0;i< document.forms[0][7].value.length;i++) { // le caractuel est stocké dans c.
charz = document.forms[0][7].value.charAt(i);
if ((charz < "0") || (charz > "9")) {
message +="* Invalid Credit card number \n";
id++;
i=document.forms[0][7].value.length;
}}}
if (document.forms[0][8].value.length != 0) {
for(i=0;i< document.forms[0][8].value.length;i++) { // le caractuel est stocké dans c.
charz = document.forms[0][8].value.charAt(i);
if ((charz < "0") || (charz > "9")) {
message +="* Invalid Credit card expiry day \n";
id++;
i=document.forms[0][8].value.length;
}}}
if (document.forms[0][9].value.length != 0) {
for(i=0;i< document.forms[0][9].value.length;i++) { // le caractuel est stocké dans c.
charz = document.forms[0][9].value.charAt(i);
if ((charz < "0") || (charz > "9")) {
message +="* Invalid Credit card expiry month \n";
id++;
i=document.forms[0][9].value.length;
}}}
if (document.forms[0][10].value.length != 0) {
for(i=0;i< document.forms[0][10].value.length;i++) { // le caractuel est stocké dans c.
charz = document.forms[0][10].value.charAt(i);
if ((charz < "0") || (charz > "9")) {
message +="* Invalid Credit card expiry year \n";
id++;
i=document.forms[0][10].value.length;
}}}
if (document.forms[0][11].value.length != 0) {
if ((document.forms[0][11].value.indexOf("@")<0)||(document.forms[0][11].value.indexOf(".")<0)) {
message += "* Invalid email address \n";
id++;
}}
if (id!=0) {
message+=" Thank you.";
alert(message);
message="";
id=0;
return false;
 }
else {
alert(" Thank you.");
return true;
}}
//-->
</script>
Note that char is a reserved word and cannot be used as a variable... i have popped a 'z' on the end.
birdbrain is offline   Add to del.icio.us Add to del.icio.us    Can you digg it?Can you digg it? Reply With Quote
Old 09-26-2005, 12:32 AM
  #5
jp_sympa
Aspirant (Level 2)
 
Join Date: Sep 2005
Posts: 12
iTrader: (0)
jp_sympa is an unknown quantity at this point
Thanks mate, you made my day...
Didn't know about the "char" character, used to work on a previous assignment.

Cheers !!!

Last edited by jp_sympa : 09-26-2005 at 12:35 AM.
jp_sympa is offline   Add to del.icio.us Add to del.icio.us    Can you digg it?Can you digg it? Reply With Quote

Reply
KEEP TABS
SPONSORS
 
Boxedart
 
 


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



 
  POSTING RULES
 
 
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Thread Tools
Display Modes

Forum Jump

 

All times are GMT -5. The time now is 07:44 PM.

   

Mascot team created by Drawshop.com

Powered by vBulletin® Version 3.6.7
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.

Server Monitoring by ENIACmonitor 0.01
HTMLforums.com © Big Resources, Inc. Web Design by BoxedArt.com
vRewrite 1.5 beta SEOed URLs completed by Tech Help Forum and Chalo Na.