PDA

Click to See Complete Forum and Search --> : form validation w/ javascript


pnj
Oct 28th, 2000, 04:02 PM
hello,
I have a form w/ 15 items on it.
of those 15 four are Address,City,State,and Zip.
I need to make sure that if the user enters just the city or state or zip or address that I give them an alert that says "fill out the rest of the address".

right now I am using a TON of if statement. is there a way I can put those four fields into an array and loop through them for the validation?

if they don't fill out any of the four fields that is fine but if they fill out just one then I want to tell them to fill out all of them.

thanks.

monte96
Oct 28th, 2000, 11:44 PM
If you give them all the same name. You will then need to access them from the request object once you submit the form like a collection item:

Request.Form("MyTextBox")(0)
Request.Form("MyTextBox")(1)

You can access them in code like:

for(i=0; i < 15; i++)
if(MyTextBox(i).value='')
window.alert("Fill them all out");

(Check my syntax on this Javascript stuff tho!)

pnj
Oct 28th, 2000, 11:54 PM
this is just an HTML page.
could I do the same thing? or something simalar with just javascript?
I need it to be client side validation.I thought I saw this done before but maybe it was using ASP.
dunno

thanks?

Oct 30th, 2000, 02:11 AM
Hi, you could do something like this:

----------------------------------------

<!--
function checkme(myform)
{
if (myform.Name.value == "")
{
alert("The field Name must be filled in!");
myform.Name.focus();
return;
}
if (myform.Address.value == "")
{
alert("The field Address must be filled in!");
myform.Address.focus();
return;
}
if (myform.ZipCode.value == "")
{
alert("The field Zip Code must be filled in!");
myform.ZipCode.focus();
return;
}
if (myform.City.value == "")
{
alert("The field City must be filled in!");
myform.City.focus();
return;
}
if (myform.State.value == "")
{
alert("The field State must be filled in!");
myform.State.focus();
return;
}
if (myform.Country.value == "")
{
alert("The field Country must be filled in!");
myform.Country.focus();
return;
}
return true;
}

--------------------------------------------

You would of course call this function when the submit button is clicked.

pnj
Oct 30th, 2000, 08:35 AM
that is what I am doing right now.
but becouse it it not neccasary for the user to fill out the address I don't want to tell then to fill it unless they opt to (say they fill out just the city, or just the state)I
want to then alert them to fill out the rest of the address.

this is what it looks like now:
if
(myform.address.value !=0 && myform.city.value == 0 && myform.state.value ==0 && myform.zip.value == 0)
{
alert("fill out the city)"
}

I do this four times for each field. the user only gets the alert if they forget to fill out one of the fields.

thanks