p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


  Return to Index  

asp_databases thread: RE: Form Validation


Message #1 by "Drew, Ron" <RDrew@B...> on Mon, 17 Jun 2002 23:30:12 -0400
First if you are executing checkrequired using onblur on a form element,
move it to onsubmit with using a return.
Next change the return false to only execute after you have tested all
the elements.  Maybe set a variable to whatever you want to show no data
and test for it at the end returning false if any are left empty.  At
the same time, change the color to red on each element if data missing
and just alert the user is the return is false to fill in the elements
colored red.

-----Original Message-----
From: imran.saleem@b... [mailto:imran.saleem@b...]
Sent: Monday, June 17, 2002 12:59 PM
To: ASP Databases
Subject: [asp_databases] Form Validation


Hi,
 i am really new to JavaScript and i would love some help. I got the
folllowing code from a JS WebSite. It basically does everything i want
it
to. i.e. Check the fields in a form if they are empty an alert box is
generated. However the code only generates one alert at a time for each
empty form element. I want it to generate an alert listing every form
element that is blank. So for example if the Forename and Surname fields

are empty i want the alert to say the forename and Surname fields are
empty instead of just informing users that the Forename field is empty.

Heres the code. Thanks for your help.

<SCRIPT LANGUAGE=3D"JavaScript">

function checkrequired(FormElement) {
	cars =3D new Array("Mercedes", "Ford", "Chrysler")
	var pass=3Dtrue;
	for (i=3D0;i<FormElement.length;i++) {
		var xFormElem=3DFormElement.elements[i];
		if (xFormElem.name.substring(0,1)=3D=3D"$") {
			if
(((xFormElem.type=3D=3D"text"||xFormElem.type=3D=3D"textarea")&&
			xFormElem.value=3D=3D'')||(xFormElem.type.toString
().charAt(0)=3D=3D"s"&&
			xFormElem.selectedIndex=3D=3D0)) {
			pass=3Dfalse;
			break;
			}
		}
	}
	if (!pass) {
		FieldName =3D
xFormElem.name.substring(1,30).toUpperCase();
		alert(cars + FieldName + " field was properly
completed.");
	return false;
	}
	else
	return true;
}

</SCRIPT>  
Message #2 by Greg Griffiths <greg.griffiths@g...> on Mon, 17 Jun 2002 22:27:31 +0100
You should really post this in one of the Javascript lists on P2P :

<SCRIPT LANGUAGE="JavaScript">

function checkrequired(FormElement) {
         cars = new Array("Mercedes", "Ford", "Chrysler")
         var pass=true;
         for (i=0;i<FormElement.length;i++)
         {
                 var xFormElem=FormElement.elements[i];
                 if (xFormElem.name.substring(0,1)=="$")
                 {
                         if 
(((xFormElem.type=="text"||xFormElem.type=="textarea")&&xFormElem.value=='')||(xFormElem.type.toString().charAt(0)=="s"&
& 
xFormElem.selectedIndex==0)) {
                                 pass=false;
                                 FieldName = 
xFormElem.name.substring(1,30).toUpperCase();
                                 alert(cars + FieldName + " field was 
properly completed.");
                                 break;
                         }
                 }
         }
         if (!pass)
         {
                 return false;
         }
         else
         {
                 return true;
         }
}
</SCRIPT>

At 16:59 17/06/02 +0000, you wrote:
>Hi,
>  i am really new to JavaScript and i would love some help. I got the
>folllowing code from a JS WebSite. It basically does everything i want it
>to. i.e. Check the fields in a form if they are empty an alert box is
>generated. However the code only generates one alert at a time for each
>empty form element. I want it to generate an alert listing every form
>element that is blank. So for example if the Forename and Surname fields
>are empty i want the alert to say the forename and Surname fields are
>empty instead of just informing users that the Forename field is empty.
>
>Heres the code. Thanks for your help.
>
><SCRIPT LANGUAGE="JavaScript">
>
>function checkrequired(FormElement) {
>         cars = new Array("Mercedes", "Ford", "Chrysler")
>         var pass=true;
>         for (i=0;i<FormElement.length;i++) {
>                 var xFormElem=FormElement.elements[i];
>                 if (xFormElem.name.substring(0,1)=="$") {
>                         if
>(((xFormElem.type=="text"||xFormElem.type=="textarea")&&
>                         xFormElem.value=='')||(xFormElem.type.toString
>().charAt(0)=="s"&&
>                         xFormElem.selectedIndex==0)) {
>                         pass=false;
>                         break;
>                         }
>                 }
>         }
>         if (!pass) {
>                 FieldName = xFormElem.name.substring(1,30).toUpperCase();
>                 alert(cars + FieldName + " field was properly completed.");
>         return false;
>         }
>         else
>         return true;
>}
>
></SCRIPT>


Message #3 by "Kim Iwan Hansen" <kimiwan@k...> on Mon, 17 Jun 2002 21:42:54 +0200
Hiya,

Not to be bitchy in any way, but isn't there a javascript forum too at
p2p.wrox.com?

-Kim

> -----Original Message-----
> From: imran.saleem@b... [mailto:imran.saleem@b...]
> Sent: 17. juni 2002 16:59
> To: ASP Databases
> Subject: [asp_databases] Form Validation
>
>
> Hi,
>  i am really new to JavaScript and i would love some help. I got the
> folllowing code from a JS WebSite. It basically does everything i want it
> to. i.e. Check the fields in a form if they are empty an alert box is
> generated. However the code only generates one alert at a time for each
> empty form element. I want it to generate an alert listing every form
> element that is blank. So for example if the Forename and Surname fields
> are empty i want the alert to say the forename and Surname fields are
> empty instead of just informing users that the Forename field is empty.
>
> Heres the code. Thanks for your help.
>
> <SCRIPT LANGUAGE="JavaScript">
>
> function checkrequired(FormElement) {
> 	cars = new Array("Mercedes", "Ford", "Chrysler")
> 	var pass=true;
> 	for (i=0;i<FormElement.length;i++) {
> 		var xFormElem=FormElement.elements[i];
> 		if (xFormElem.name.substring(0,1)=="$") {
> 			if
> (((xFormElem.type=="text"||xFormElem.type=="textarea")&&
> 			xFormElem.value=='')||(xFormElem.type.toString
> ().charAt(0)=="s"&&
> 			xFormElem.selectedIndex==0)) {
> 			pass=false;
> 			break;
> 			}
> 		}
> 	}
> 	if (!pass) {
> 		FieldName = xFormElem.name.substring(1,30).toUpperCase();
> 		alert(cars + FieldName + " field was properly completed.");
> 	return false;
> 	}
> 	else
> 	return true;
> }
>
> </SCRIPT>

Message #4 by =?iso-8859-1?Q?Andr=E9_Van_Butsel?= <avanbutsel@b...> on Mon, 17 Jun 2002 20:06:06 +0200
Hi Imran
You have to look in the html of your form
I assume every field calls the function checkrequired "onblur" (= on leaving
the field). You have to remove these calls near every field and call the
function with the submit button "onsubmit"
André
----- Original Message -----
From: <imran.saleem@b...>
To: "ASP Databases" <asp_databases@p...>
Sent: Monday, June 17, 2002 4:59 PM
Subject: [asp_databases] Form Validation


> Hi,
>  i am really new to JavaScript and i would love some help. I got the
> folllowing code from a JS WebSite. It basically does everything i want it
> to. i.e. Check the fields in a form if they are empty an alert box is
> generated. However the code only generates one alert at a time for each
> empty form element. I want it to generate an alert listing every form
> element that is blank. So for example if the Forename and Surname fields
> are empty i want the alert to say the forename and Surname fields are
> empty instead of just informing users that the Forename field is empty.
>
> Heres the code. Thanks for your help.
>
> <SCRIPT LANGUAGE="JavaScript">
>
> function checkrequired(FormElement) {
> cars = new Array("Mercedes", "Ford", "Chrysler")
> var pass=true;
> for (i=0;i<FormElement.length;i++) {
> var xFormElem=FormElement.elements[i];
> if (xFormElem.name.substring(0,1)=="$") {
> if
> (((xFormElem.type=="text"||xFormElem.type=="textarea")&&
> xFormElem.value=='')||(xFormElem.type.toString
> ().charAt(0)=="s"&&
> xFormElem.selectedIndex==0)) {
> pass=false;
> break;
> }
> }
> }
> if (!pass) {
> FieldName = xFormElem.name.substring(1,30).toUpperCase();
> alert(cars + FieldName + " field was properly completed.");
> return false;
> }
> else
> return true;
> }
>
> </SCRIPT>
>

Message #5 by imran.saleem@b... on Mon, 17 Jun 2002 16:59:18
Hi,
 i am really new to JavaScript and i would love some help. I got the 
folllowing code from a JS WebSite. It basically does everything i want it 
to. i.e. Check the fields in a form if they are empty an alert box is 
generated. However the code only generates one alert at a time for each 
empty form element. I want it to generate an alert listing every form 
element that is blank. So for example if the Forename and Surname fields 
are empty i want the alert to say the forename and Surname fields are 
empty instead of just informing users that the Forename field is empty. 

Heres the code. Thanks for your help.

<SCRIPT LANGUAGE="JavaScript">

function checkrequired(FormElement) {
	cars = new Array("Mercedes", "Ford", "Chrysler")
	var pass=true;
	for (i=0;i<FormElement.length;i++) {
		var xFormElem=FormElement.elements[i];
		if (xFormElem.name.substring(0,1)=="$") {
			if 
(((xFormElem.type=="text"||xFormElem.type=="textarea")&&
			xFormElem.value=='')||(xFormElem.type.toString
().charAt(0)=="s"&&
			xFormElem.selectedIndex==0)) {
			pass=false;
			break;
			}
		}
	}
	if (!pass) {
		FieldName = xFormElem.name.substring(1,30).toUpperCase();
		alert(cars + FieldName + " field was properly completed.");
	return false;
	}
	else
	return true;
}

</SCRIPT>   

  Return to Index