Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Building Logic into a Form 2

Status
Not open for further replies.

sborny

Technical User
Jun 29, 2001
157
GB
Hi,

I am trying to build some validation and logic into a web form.

The problem I have is that I know nothing about scripting of any kind.

I need to check that certain fields have information entered as the validation and to have logic that checks if field A has a value in it then field B & C do not require an entry.

I know that this is probably very easy to all you guys and gals who know scripting, but any help would be really appreciated.

Please note that some of the fields will need to be drop down boxes.

I hope some one can help.

Thanks.

S.

Everything has an answer, it's just knowing the right question to ask. !!!!
 
Hi WartookMan,

I think that I have cracked it.

I moved the if blank code to just above the if(!invalidEmail || !invalidPhone) { code and added an else and it seems to work. I have not finished testing it so I will let you know if this post was premature.

Could you just double check the code for me.

<!--
function formValidateEmail(email) {
var regex = /^[a-zA-Z0-9._-]+@([a-zA-Z0-9.-]+\.)+[a-zA-Z0-9.-]{2,4}$/;
return regex.test(email);
}
function VerifyData() {
var msg1=&quot;The following fields must be completed:&quot;;
var blank = false;
var msg2=&quot;At least one of the following must be completed:\n* Email\n* Phone&quot;;
var invalidEmail=false, invalidPhone=false;
for(var x=0;x<document.forms[0].length;x++) {
theObj=document.forms[0].elements[x];
theType=theObj.type;
theName=theObj.name;
theValue=theObj.value;
if((theType!=&quot;SUBMIT&quot;)&&(theType!=&quot;RESET&quot;)) {
// check if the field is the Email address and validate
if( ((theName==&quot;Title&quot;)&&(theObj.selectedIndex==0)) ||
((theName==&quot;Forename&quot;)&&(theValue==&quot;&quot;)) ||
((theName==&quot;Surname&quot;)&&(theValue==&quot;&quot;)) ) {
msg1+=&quot;\n* &quot;+theName;
blank = true;




}


if( (theName==&quot;txtEmail&quot;)&&(!formValidateEmail(theValue)) ) {
invalidEmail=true;
alert(&quot;Invalid email&quot;);
}
if( (theName==&quot;txtPhone&quot;)&&(theValue==&quot;&quot;) ){
invalidPhone=true;
}
}
}
if(blank) {
alert(msg1);
//return false;
}else {

if(!invalidEmail || !invalidPhone) {
alert(&quot;Form ok - submit it!&quot;);
// return true;
return false;
} else {
alert(&quot;Either the phone or a valid email address must be entered&quot;);
return false;
}
}
}

//-->


Once again thanks for all your help. As you can see I learn far better if I have something to work with. I am much more a hands on rather than a reader of books.

Cheers

Symon.


Everything has an answer, it's just knowing the right question to ask. !!!!
 
One last note,

in the last section that checks if the email and phone are correct you had set it to return false even if it was okay.

That stopped the form actually be processed.

Once I had changed it to set as true it works a treat.

Now all I have to do is learn how to create a CSV file from the contents of the form and e-mail it.

Thanks again.

I have given you another star as you deserve it.

Cheers

Symon.

Everything has an answer, it's just knowing the right question to ask. !!!!
 
Hi WartookMan,

I am know pulling my hair out.

I have edited the code you supplied so that it lists only the fields that are blank. That works great.

I have been trying to get it to check if a value in a field is less than 3 and if one of 3 phone number fields is entered.

It seems to skip the 2 parts I have entered and just produce the list of empty fields.

I am obviously missing something. I have searched the internet for the syntax of loops and if statements in Javascript and I thought that I had done it correctly.

Here is the code.

<!--
//function formValidateEmail(email) {
// var regex = /^[a-zA-Z0-9._-]+@([a-zA-Z0-9.-]+\.)+[a-zA-Z0-9.-]{2,4}$/;
//return regex.test(email);
//}
function VerifyData() {
var msg1=&quot;The following fields must be completed:&quot;;
var blank = false;
var msg2=&quot;At least one of the following must be completed:\n* Email\n* Phone&quot;;
var invalidPhone=false;
for(var x=0;x<document.forms[0].length;x++) {
theObj=document.forms[0].elements[x];
theType=theObj.type;
theName=theObj.name;
theValue=theObj.value;
if((theType!=&quot;SUBMIT&quot;)&&(theType!=&quot;RESET&quot;)) {
// check if the field is the Email address and validate
if( ((theName==&quot;Title&quot;)&&(theObj.selectedIndex==0)) ||
((theName==&quot;Forename&quot;)&&(theValue==&quot;&quot;)) ||
((theName==&quot;Initial&quot;)&&(theValue==&quot;&quot;)) ||
((theName==&quot;Surname&quot;)&&(theValue==&quot;&quot;)) ||
((theName==&quot;Loan_Amount&quot;)&&(theObj.selectedIndex==0)) ||
((theName==&quot;Loan_Purpose&quot;)&&(theObj.selectedIndex==0)) ||
((theName==&quot;DOB&quot;)&&(theValue==&quot;&quot;)) ||
((theName==&quot;Sex&quot;)&&(theObj.selectedIndex==0)) ||
((theName==&quot;Marital_Status&quot;)&&(theObj.selectedIndex==0)) ||
((theName==&quot;No_of_Children&quot;)&&(theValue==&quot;&quot;)) ||
((theName==&quot;House_Number&quot;)&&(theValue==&quot;&quot;)) ||
((theName==&quot;House_Name&quot;)&&(theValue==&quot;&quot;)) ||
((theName==&quot;Flat_Number&quot;)&&(theValue==&quot;&quot;)) ||
((theName==&quot;Street_Name&quot;)&&(theValue==&quot;&quot;)) ||
((theName==&quot;District&quot;)&&(theValue==&quot;&quot;)) ||
((theName==&quot;Town&quot;)&&(theValue==&quot;&quot;)) ||
((theName==&quot;County&quot;)&&(theValue==&quot;&quot;)) ||
((theName==&quot;Postcode&quot;)&&(theValue==&quot;&quot;)) ||
((theName==&quot;Years_at_Address&quot;)&&(theValue==&quot;&quot;)) ||
((theName==&quot;Months_at_Address&quot;)&&(theValue==&quot;&quot;)) ||
//((theName==&quot;Home_Phone&quot;)&&(theValue==&quot;&quot;)) ||
//((theName==&quot;Mobile_Phone&quot;)&&(theValue==&quot;&quot;)) ||
//((theName==&quot;Daytime_Phone&quot;)&&(theValue==&quot;&quot;)) ||
((theName==&quot;Employers_Name&quot;)&&(theValue==&quot;&quot;)) ||
((theName==&quot;Employers_Address&quot;)&&(theValue==&quot;&quot;)) ||
((theName==&quot;Years_with_Employer&quot;)&&(theValue==&quot;&quot;)) ||
((theName==&quot;Months_with_Employer&quot;)&&(theValue==&quot;&quot;)) ||
((theName==&quot;Employment_Status&quot;)&&(theObj.selectedIndex==0)) ||
((theName==&quot;Net_Income&quot;)&&(theValue==&quot;&quot;)) ||
((theName==&quot;Are_you_Paid&quot;)&&(theObj.selectedIndex==0)) ||
((theName==&quot;Surname&quot;)&&(theValue==&quot;&quot;)) ) {
msg1+=&quot;\n* &quot;+theName;
blank = true;
}
}
}


if ((theName==&quot;Years_at_Address&quot;)&&(theValue<=&quot;3&quot;)){
alert(&quot;Please enter a previous address&quot;);
}

if( (theName==&quot;Home_Phone&quot;)&&(theValue==&quot;&quot;) ||

(theName==&quot;Mobile_Phone&quot;)&&(theValue==&quot;&quot;) ||

(theName==&quot;Daytime_Phone&quot;)&&(theValue==&quot;&quot;) ){
invalidPhone=true;

}else{
invalidPhone=false;}



if(blank) {
alert(msg1);
return false;{
}

if(!(invalidPhone)) {
return true;

}else {
alert(&quot;Please enter one of the three phone numbers required.&quot;);
return false;
}
}
}

//-->

Please help.

Thanks

Symon.

Everything has an answer, it's just knowing the right question to ask. !!!!
 
Can you mail me the full page, with the FORM in it - I think my email address before was wrong. Try: buk_nayked(at)yahoo.com (not .co.uk as before).

You seem to have a couple of extra curly braces &quot;}&quot; &quot;{&quot; in your code - one at the very bottom of the script, and one near the &quot;if(blank)...&quot; check.

Please (when you post code) use the [ignore]
Code:
...
[/ignore]
wrappers, so that the text is mono-spaced. It is much easier to read, and to paste into a text editor. It makes matching up your indents, SO much easier as well. :)

Pete.


Lotus Notes Web Developer / Aptrix (LWWCM) Consultant
w: e: Pete.Raleigh(at)lclimited.co.uk
 
Pete,

I have e-mailed you the page.

I thought that about the brackets. But if I remove one or some i get error messages.

Cheers for the help.

Symon.

Everything has an answer, it's just knowing the right question to ask. !!!!
 
Received and testing... it might just be the brackets. You can also save yourself a lot of code by using style sheets (CSS). I'll fix you up first, and then we can look at CSS examples.

You can test as well by:
1. Deleting one of the last right-facing curly brackets &quot;}&quot; at the end of the JavaScript - you have one too many.
2. Deleting another within the &quot;if(blank)...&quot; statement, so it looks like this - notice the extra bracket at the end of your &quot;return false;&quot; statement (in YOUR code). It should be:
Code:
...
	if(blank) {
		alert(msg1);
		return false; // <- Remove the curly from here!
	}
...

I'll see if there are any more bugs. ;-)

Pete.


Lotus Notes Web Developer / Aptrix (LWWCM) Consultant
w: e: Pete.Raleigh(at)lclimited.co.uk
 
Hi Pete,

I have removed the brackets abd I don't get any error messages, but it still does not rough up the message about the address or the phone numbers.

Have you had any more luck your end.

By the way, where are you from?

Cheers

Symon.

Everything has an answer, it's just knowing the right question to ask. !!!!
 
by the way, sorry about the form in the coding. Not sure what you are meant to indent and what not.

I will remember the code tag in future.

Cheers

Symon.

Everything has an answer, it's just knowing the right question to ask. !!!!
 
Originally, Melbourne (Oz) - yes, another Aussie in London - not one of the &quot;ocker&quot; ones though [thumbsup2]. Lived in London for 3+ years.

Mail sent - you need to add validation on the house number and name. I had to enter info into the house number, house name, and flat number to get it to submit.

Let me know how you get on. Might head to bed. :)

Pete.


Lotus Notes Web Developer / Aptrix (LWWCM) Consultant
w: e: Pete.Raleigh(at)lclimited.co.uk
 
Pete,

I was wondering if you were mad like me and still doing this stuff at this time of night.

I thought I had validation on every field.

I was trying to get it to check if the years at addres was less than three years and also to check if any 1 of the 3 phone numbers had been entered.

The main validation on the general fields works fine it was just the 2 i mention above.

Did you get my second e-mail

Cheers

Symon.

Everything has an answer, it's just knowing the right question to ask. !!!!
 
The mail I sent was a modified version. I'll try your above requirements and send you another copy for you to test.

You do seem to have validation on every field - that seems to be the problem. Example: I might live at 34 East Street. But I have no Flat No. and I have no House Name. Yet, they are required to be non-blank. :-(

Pete.


Lotus Notes Web Developer / Aptrix (LWWCM) Consultant
w: e: Pete.Raleigh(at)lclimited.co.uk
 
Pete,

point taken on the house name / number. I forgot about that one.

The e-mail I got from you appeared as the form in my preview pane and there was a text file attached that was your message.

cheers

Symon.

Everything has an answer, it's just knowing the right question to ask. !!!!
 
I've had another play with the code and got a little further. I've added one or two comments to read as well.

You might have to break it up into separate chunks of validation... ie. Presonal Info, the Previous Address Information, the Contact Details (phones).

I'll ZIP what I have and mail it to you.

Pete.


Lotus Notes Web Developer / Aptrix (LWWCM) Consultant
w: e: Pete.Raleigh(at)lclimited.co.uk
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top