Reply
Making fields mandatory:
Old 03-25-2008, 11:10 AM Making fields mandatory:
rolda hayes's Avatar
Wannabe Adventurer...

Posts: 553
Name: Darren
Location: England
Hi there,

Please have a look at my code for me,
It is supposed to make most of the fields mandatory but at the moment it lets
the form go with out any credit card info... is there something obvious I have missed?
Code:
function validateForm(form)
{ 
var Card_ExpMon = form.Card_ExpMon.value;
var expyr = form.Card_ExpYear.value;
var temp = new Date();
var nowMonth  = temp.getMonth();
nowMonth ++;
var nowYear = temp.getFullYear(); 
var Card_CardType = form.Card_CardType.value;
var emailID = form.Customer_Email.value;
//alert(Card_ExpMon);
//alert(expyr);
//alert(nowMonth);
//alert(nowYear);
//alert(Card_CardType);

	if (form.Customer_Name.value == "")
	{ 
	   alert("Please Enter First Name."); 
	   form.Customer_Name.focus( ); 
	   return false; 
	}
	else if (form.Customer_Surname.value == "")
	{ 
	   alert("Please Enter Your Surname."); 
	   form.Customer_Surname.focus( ); 
	   return false; 
	}
	else if (form.Customer_Address1.value == "")
	{ 
	   alert("Please Enter Your Address."); 
	   form.Customer_Address1.focus( ); 
	   return false; 
	}
	else if (form.Customer_Town.value == "")
	{ 
	   alert("Please Enter Your Town."); 
	   form.Customer_Town.focus( ); 
	   return false; 
	}
	else if (form.Customer_County.value == "")
	{ 
	   alert("Please Enter Your County."); 
	   form.Customer_County.focus( ); 
	   return false; 
	}
	else if (form.Customer_Postcode.value == "")
	{ 
	   alert("Please Enter Your Postcode."); 
	   form.Customer_Postcode.focus( ); 
	   return false; 
	}
	else if ((form.Customer_Email.value==null)||(form.Customer_Email.value=="")){
		alert("Please Enter your Email address")
		form.Customer_Email.focus()
		return false
	}
	else if (echeck(form.Customer_Email.value)==false){
		form.Customer_Email.value=""
		form.Customer_Email.focus()
		return false
	}

	else if (form.Customer_Tel.value == "")
	{ 
	   alert("Please Enter Your Telephone Number."); 
	   form.Customer_Tel.focus( ); 
	   return false; 
	}
	else if (form.Card_Country.value == "-")
	{ 
	   alert("Please Enter Your Country."); 
	   form.Card_Country.focus( ); 
	   return false; 
	}
	else if (form.Card_CardType.value == "-")
	{ 
	   alert("Please Enter Your Card Type."); 
	   form.Card_CardType.focus( ); 
	   return false; 
	}
	else if (form.Card_Nameoncard.value == "")
	{ 
	   alert("Please Enter Name on Card.");
	   form.Card_Nameoncard.focus( ); 
	   return false; 
	}
	else if (form.Card_Cardno.value == "")
	{ 
	   alert("Please Enter Card Number."); 
	   form.Card_Cardno.focus( ); 
	   return false; 
	}
	else if (isNaN(form.Card_Cardno.value))
	{ 
	   alert("Card Number must be a valid number."); 
	   form.Card_Cardno.focus( ); 
	   return false; 
	}
	else if (form.Card_Cardno.value.length < 16)
	{ 
	   alert("Please Enter Valid Card Number with no spaces."); 
	   form.Card_Cardno.focus( ); 
	   return false; 
	}
	
	//perform verification on issue dates and verification number depending on card type
	switch(form.Card_CardType.value) { 
		case 'Switch': 
			if ((form.Card_IssueMon.value == "-" ) || (form.Card_IssueMon.value == ""))
			   {
			   //alert(form.Card_Verifyno.value);
			   alert("Enter the month your card was issued."); 
			   form.Card_IssueMon.focus( ); 
			   return false;
			   }
			else if ((form.Card_IssueYear.value == "-" ) || (form.Card_IssueYear.value == ""))
			   {
			   //alert(form.Card_Verifyno.value);
			   alert("Enter the year your card was issued."); 
			   form.Card_IssueYear.focus( ); 
			   return false;
			   }		
	    break;

		case 'MasterCard': 
	    break;
		
		case 'VisaCard': 
		break;
	}		
	if ((nowYear > expyr) || ((nowYear == expyr ) && (nowMonth > Card_ExpMon)))
	{ 
	   alert("Expiry Date has expired."); 
	   form.Card_ExpMon.focus( ); 
	   return false; 
	}
	else if (form.Card_Verifyno.value == "")
   {
	   //alert(form.Card_Verifyno.value);
	   alert("Please enter your verification number."); 
	   form.Card_Verifyno.focus( ); 
	   return false;
   }
	else if (isNaN(form.Card_Verifyno.value))
	   { 
	   alert("Your verification Number must be a valid number."); 
	   form.Card_Verifyno.focus( ); 
	   return false; 
	   }
	else if (form.Card_Verifyno.value.length != 3)
	{ 
	   alert("Your verification Number must be 3 digits."); 
	   form.Card_Verifyno.focus( ); 
	   return false; 
	}  
	else if (form.Customer_Tel.value == "")
	{ 
	   alert("Please Enter Your Telephone Number."); 
	   form.Customer_Tel.focus( ); 
	   return false; 
	}
}

//email validation function, called from within above validateForm function
function echeck(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   alert("The E-mail that you have entered is not valid")
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   alert("The E-mail that you have entered is not valid")
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    alert("The E-mail that you have entered is not valid")
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    alert("The E-mail that you have entered is not valid")
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    alert("The E-mail that you have entered is not valid")
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    alert("The E-mail that you have entered is not valid")
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    alert("The E-mail that you have entered is not valid")
		    return false
		 }

 		 return true					
	}
__________________
"I always wanted the adoration of John Lennon - With The Anonimity of Ringo Starr..."
QuizBay Help with the testing of this Beta site!
rolda hayes is offline
Reply With Quote
View Public Profile
 
When You Register, These Ads Go Away!
     
Old 03-28-2008, 06:40 PM Re: Making fields mandatory:
Novice Talker

Posts: 11
Name: Tortoise
Problem may not be with the function, but how you're calling it. Are you doing this:

<form action="..." method="..." onSubmit="return validateForm(this)"> ?
Tortoise is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Making fields mandatory:
 

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

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




   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML

 


Page generated in 0.10477 seconds with 13 queries