Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

JavaScript Forum


You are currently viewing our JavaScript Forum as a guest. Please register to participate.
Login



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

Posts: 960
Name: Darren
Location: England
Trades: 0
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 Just a test to see what happens...
Please login or register to view this content. Registration is FREE

"Let us be thankful for the fools. But for them the rest of us could not succeed..."
rolda hayes is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 03-28-2008, 07:40 PM Re: Making fields mandatory:
Novice Talker

Posts: 11
Name: Tortoise
Trades: 0
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 Search this Thread
Search this Thread:

Advanced Search

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 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.09777 seconds with 12 queries