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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Applying conditions to form verifications

Status
Not open for further replies.

howdoesthiswork

Technical User
Sep 24, 2002
18
0
0
NA
Hy again

I have created a html form that needs verification depending on what was completed. For arguments sake the visitor has to fill in his Name and then has a choice of either submitting his email or phone number. i. submitting name & phone is ok, as well as submitting name and email. Submitting only one of the three variables prompts him to complete those ... does this make sense?
 
on submitting the form: onSubmit = "return validate();"

Code:
<SCRIPT>
function validate()
{
 var message = &quot;&quot;;
 var name = form1.namebox.value;
 //whatever validation you want here.

 if(name == &quot;&quot;)
  message += &quot;Please enter your name before continuing.\n&quot;;

 var phone = form1.phonebox.value;
 //whatever validation you want here.

 var email = form1.emailbox.value;
 //whatever validation you want here.

 if(phone==&quot;&quot; && email==&quot;&quot;)
  message += &quot;You must enter either a valid phone number or email address before continuing.&quot;;

 if(phone!=&quot;&quot; && email!=&quot;&quot;)
  message += &quot;Please only enter EITHER a valid phone number OR a valid email address.\n&quot;; //if you want

 if(message != &quot;&quot;)
 {
  alert(message);
  return false;
 }
}
</SCRIPT>

Is this what you're after?

Good luck.

--Dave
 
Yep that's about it, one question though. If I would want to include more fields that have to be completed (same as name above) could I do the following

function validate()
{
var message = &quot;&quot;;
var name = form1.namebox.value;
var surname= form1.surnamebox.value;

if(name == &quot;&quot;)
if(surname == &quot;&quot;)
message += &quot;Please enter your name and surname before continuing.\n&quot;;

or am I missing something obvious here? Similar if I would want to have him complete one of three possible fields (phone, email or address).
 
Ok, wait, I did the following and it seems to work fine ...

function validate()
{
var message = &quot;&quot;;
var name = form1.namo.value;
var surname = form1.surnamo.value;
//whatever validation you want here.

if(name == &quot;&quot;)
message += &quot;Please enter your name before continuing.\n&quot;;
if(surname == &quot;&quot;)
message += &quot;Please enter your surname before continuing.\n&quot;;

I suppose the variable form fields can be added in a similar fashion?
 
Yes. That is fine.

The process might be &quot;simplified&quot; if you had two same-sized array. The first would have the name of the field you want filled in and the other would have the user-friendly name. Then you could do something like this:

Code:
for(i=0; i<fieldNames.length; i++)
{
 if(form1.fieldNames[i].value == &quot;&quot;)
  message += &quot;Please enter a &quot; + userFriendlyNames[i] + &quot; before continuing.\n&quot;;
}

This precludes the possibility of doing an either/or comparison as for the email OR phone number thing, but you can still do that manually after the for-loop.

Have fun!

--Dave
 
Oops. I didn't really finish my thought, above. What I was after was that, if you add or subtract form fields, you would only have to update your arrays to include (or eliminate) the check for these fields. It might make it easier to update is all.

--Dave
 
That is way beyond my current javascript knowledge or abilities, I think ....
*blushing bright red, he raises his hand* ... what are arrays and userFriendlyNames?
 
I just made up the name &quot;userFriendlyNames&quot;. A VERY quick and dirty on arrays is that they are one variable that can hold many pieces of data. If an array held the names of the seven dwarves, for example, you might have something like:

var dwarves = new Array(&quot;Happy&quot;,&quot;Sneezy&quot;,&quot;Bashful&quot;,&quot;Doc&quot;,&quot;Grumpy&quot;,&quot;Sleepy&quot;,&quot;Dopey&quot;);

Then, dwarves[0] would be the same as saying &quot;Happy&quot;, dwarves[1] would be the same as &quot;Sneezy&quot; ... and dwarves[6] would be the same as &quot;Dopey&quot; (notice that arrays are zero-based, so an array of seven elements have index numbers of 0 through 6).

You could then make their names show up on the screen in order with this simple javascript:

Code:
for(i=0;i<dwarves.length;i++)
 alert(dwarves[i]);

In this case, dwarves.length is automatically equal to 7 because dwarves has seven elements.

There's more, but that'll get you started.

I was just supposing, that if your form was going to change over time, that you might consider using arrays as such:

Code:
<SCRIPT>
var fieldNames = new Array(&quot;namo&quot;, &quot;surnamo&quot;, &quot;cito&quot;, &quot;stato&quot;, &quot;zippo&quot;, &quot;groucho&quot;);
var userFriendlyNames = new Array(&quot;name&quot;, &quot;surname&quot;, &quot;city&quot;, &quot;state&quot;, &quot;zip code&quot;, &quot;favorite Marx Brother&quot;);

function validate()
{
 var message = &quot;&quot;;
 for(i=0; i<fieldNames.length; i++)
 {
  if(form1.fieldNames[i].value == &quot;&quot;)
   message += &quot;Please enter a &quot; + userFriendlyNames[i] + &quot; before continuing.\n&quot;;
 }

 if(message != &quot;&quot;)
 {
  alert(message);
  return false;
 }
}
</SCRIPT>

Now, if you wanted to, for example, add a pet's name field to your form as such:

Code:
Pet's Name: <input type=text name=petto size=20><BR>

...then, you wouldn't have to add a whole additional if-statement to the validate code, you need merely update your arrays in the javascript as such:

Code:
var fieldNames = new Array(&quot;namo&quot;, &quot;surnamo&quot;, &quot;cito&quot;, &quot;stato&quot;, &quot;zippo&quot;, &quot;groucho&quot;, &quot;petto&quot;);
var userFriendlyNames = new Array(&quot;name&quot;, &quot;surname&quot;, &quot;city&quot;, &quot;state&quot;, &quot;zip code&quot;, &quot;favorite Marx Brother&quot;, &quot;favorite pet's name&quot;);

Notice that all I did was add an additional value in the list of parameters in each array.

'hope this answers your questions.

--Dave
 
Dave is the man!

Thanks, I will wrap my mind around it, it does make perfect sense and is a lot shorter javascript than the way I intended doing it.

Thanx a million ...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top