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. !!!!
 
I'm sure you're not only looking to learn about validation but about server-side scripting as well right?

Research about (if you don't know already) what language you want to use (i.e. php, asp, etc...) then search for relevant information about that language.

I just searched for "server side scripting tutorials" and got these results get in the habbit of using similar phrases/key words to better help you.

There actually thousands of tutorials outthere.
Good luck...

[tt]

google.gif
 
If you click the
tabkywrd1.gif
above in the JavaScript forum and type "Validation" you will the solution to validation many times over...
 
Top idea DeCojute - a star for you ... why rewrite a similar code solution again and again and again...

Some of the search engines give fantastic results as well. I tend to use some of the following keywords preceding my requests:
"javascript ..."
"javascript form validation ..."
"javascript home webpage ..."

I guess when there are so many resources, some people don't know where to start. [thumbsup2]

Pete.


Lotus Notes Web Developer / Aptrix (LWWCM) Consultant
w: e: Pete.Raleigh(at)lclimited.co.uk
 
Thanks WartookMan.
I'm always trying to educate/point the user about already answered results. Too many "New" users are simply looking to cut & paste without effort and or interest in learning. (IMHO)
 
Hello guys,

I would like to point out that one, I am not a new user just new to the HTML forum. I have contributed a lot to the VB forum.

Secondly, I am not looking to cut and paste. As I have stated in another post in a different forum I am under a serious time constraint and am looking for a helping hand to get this done. Once it is done I will be spending alot of time learning ASP and JAVASCRIPT properly so that I don't have to keep asking questions.

I do not want to annoy anyone but I just seem to be being told to do a search in a search engine and not being offered any help for the short term in the forum. I was under the impression that forums are for people to share their knowledge and not to just send you to a generic serach engine.

I hope that this does not offend any one as it is not being written with any malice.

Cheers

Symon.

Everything has an answer, it's just knowing the right question to ask. !!!!
 
"I was under the impression that forums are for people to share their knowledge and not to just send you to a generic search engine."

Yes and no. If somebody comes here with a query that could be answered by typing the right words into Google, then that's what they may be told to do. Sometimes they might just not know the magic words to search for.

I think the forum's more valuable for solving queries which can't be solved by Googling.

-- Chris Hunt
 
Fair point chris.

However I have done the google thing and it throws so much up it is difficult to know where to start.

Cheers

Symon.

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

No offence taken. Yes - I agree. Forums are for people to share their knowledge and ultimately help someone in the process. It is best to always state what your timeframes are... much easier to manage customer expectations that way.

Hopefully the following (working example) might help:
Code:
<html>
<head>
<title>Registration Form</title>
<script language=&quot;JavaScript&quot;>
<!--
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:\n* Title\n* First name\n* Last name&quot;;
	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;txtTitle&quot;)&&(theObj.selectedIndex==0)) ||
				((theName==&quot;txtFirstName&quot;)&&(theValue==&quot;&quot;)) ||
				((theName==&quot;txtLastName&quot;)&&(theValue==&quot;&quot;)) ) {
				alert(msg1);
				return false;
			}
			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(!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;
	}
}
//-->
</script>
</head>
<body style=&quot;font-family: arial&quot;>
<H1 align=&quot;center&quot;>Example Form</H1>
<form name=&quot;frmUser&quot; onSubmit=&quot;return VerifyData();&quot;>
    Title *: <select name=&quot;txtTitle&quot;>
		<option value=&quot;&quot; selected>-</option>
		<option value=&quot;Mr&quot;>Mr</option>
		<option value=&quot;Mrs&quot;>Mrs</option>
		<option value=&quot;Miss&quot;>Miss</option>
		<option value=&quot;Ms&quot;>Ms</option>
		</select><br>
    First Name *: <input type=&quot;text&quot; name=&quot;txtFirstName&quot;><br>
    Surname *: <input type=&quot;text&quot; name= &quot;txtLastName&quot;><br>
    Email Address: <input type=&quot;text&quot; name=&quot;txtEmail&quot;><br>
    Telephone Number: <input type=&quot;text&quot; name=&quot;txtPhone&quot;><br>
    <input type=&quot;submit&quot; value=&quot;Submit&quot;>  <input type=&quot;reset&quot; value=&quot;Clear&quot;>
</form>
</body>
</html>

Pete.


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

Thank you very much.

I really appreciate the help.

I will test this in my page. Thank you for being so understanding.

I will now go away and learn what the code does so as to not cause this kind of trouble again.

Sorry to have been a pain.

Thanks again.

Symon.

Everything has an answer, it's just knowing the right question to ask. !!!!
 
Oh by the way I have given you a well deserved star WartookMan.

Cheers.

Everything has an answer, it's just knowing the right question to ask. !!!!
 
If you have any questions on the example I've given you, feel free to post them here. It might be the start of a really good FAQ - that everyone can use!

Then whenever anyone asks about anything to do with Form validation, the FAQ can be the first point of reference. [thumbsup2]

Pete.


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

I will post here. I am sure that I will have questions, however I will try to work things out before posting.

Cheers

Symon.

Everything has an answer, it's just knowing the right question to ask. !!!!
 
[tt]
&quot;&quot;I was under the impression that forums are for people to share their knowledge and not to just send you to a generic search engine.&quot;&quot;

I agree with that statement Chris 100%

But I am the type of person that likes to teach and educate the &quot;common&quot; user to be a little more creative in what they're looking for, show me that you're not &quot;just looking to cut-and-paste&quot; without the interest of learning. I personally feel that if a question has been asked many times over and an answer found, I would first point the poster to that answer and if the poster has enough interest to actually implement the code and has trouble making it work due to not understanding THEN it would require the advise/help of an expert.

I don't know how you feel about user simply asking things like &quot;how do I write a macro?&quot;, &quot;Can I display the Time in a ASP page?&quot;, &quot;What's the code for submitting a form?&quot;, etc
these to me are way too generic questions that are best answered by directing the user to an already answered post.

Here's a good read: thread717-611615 regarding what I'm trying to say.

[tt]

google.gif
 
Hi Wartookman,

I have applied the code you provided with my form and tested it.

I think I understand roughly what is going on.

One question though.

var msg1 is what gets displyed if the fields are not populated.

However if one of the 3 fields that are currently being checked is completed the message still displays that it needs to be completed.

To show that I am trying to learn I have spliced some other code I have that displays a msg and adds the name of the field that needs completing.

However it only ever displays the first field name, even if three fields need completing.

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 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(blank) {
alert(msg1);
return false;
}

return 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(!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;
}
}
//-->


This is were my lack of knowledge comes in.

As you can see I only require the names of the fields that actually need completing to be listed in the message and not a static list.

I am trying here.

Cheers

Symon.


Everything has an answer, it's just knowing the right question to ask. !!!!
 
Symon, you are setting the name of the field correctly, but if you find a blank field, you are (or I was) returning immediately once you find the first blank field... the core of it is here:

Code:
for(var x=0;x<document.forms[0].length;x++) {
...
    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(blank) {
            alert(msg1);
            return false; // THIS IS THE KEY
        }
...
    }

If you want to construct a list of them, perform the if(blank) statement outside of the FOR loop.

Pete.


Lotus Notes Web Developer / Aptrix (LWWCM) Consultant
w: e: Pete.Raleigh(at)lclimited.co.uk
 
Thinking about it further, you've actually created redundant code... I thought I should attach an example of what to do - so there's no confusion:

Code:
for(var x=0;x<document.forms[0].length;x++) {
...
    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(blank) {
    alert(msg1);
    return false;
}

Pete.


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

Thanks.
I normally program in VB and in a FOR loop there is a next statement that returns to the beginning of the loop.

I do not notice a NEXT in the script so would you be kind enough to let me know where the end of the is.

I have tried the blank code in various places and either get a return out of function error or the message comes up for the first blank field. the when i press ok it comes up again with the first and second blank field listed and so on. I have to press the ok button the same number of times as there are fields on the form which is about 40.

Any ideas

Cheers

Symon.



Everything has an answer, it's just knowing the right question to ask. !!!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top