Search the archives!
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Javascript] Re: form validation
- From: javascript@xxxxxxxxxx (Roger Roelofs)
- Subject: [Javascript] Re: form validation
- Date: Tue, 28 Jan 2003 14:44:53 -0500
--Apple-Mail-2-1051384419
Content-Transfer-Encoding: 7bit
Content-Type: text/plain;
charset=US-ASCII;
format=flowed
Craig,
Before anything else, search the net for _good_ form validation
scripts. No sense re-inventing the wheel. This will give you more
robust code than if you roll your own.
--------------
First, I don't think you need the else clauses. When a return
statement is executed the function terminates. So, maybe something
like this
> function ValidateThis(formObj) {
> var totalcount = formObj.NumberOfTables.value;
> var tableformat = formObj.TableFormat.value;
> var chairscount = formObj.NumberOfChairs.value;
> var chairsformat = formObj.ChairsFormat.value;
> var housekeepingrequest = formObj.HousekeepingRequest.checked;
> var hrd = formObj.HousekeepingRequestDate.value;
>
> if (totalcount > 1 && tableformat == "Not applicable") {
> alert("You must select a table format when you have requested 2 or
> more tables.");
> formObj.TableFormat.focus();
> return false;
> }
> if ((housekeepingrequest && hrd == "") || (housekeepingrequest &&
> (hrd.length != 10 || hrd.charAt(4) != '-' || hrd.charAt(7) != '-' ||
> (hrd.charAt(5) != 1 && hrd.charAt(5) != 0)))) {
> alert("You have selected housekeeping request. Therefore you must
> include
> a valid housekeeping request date in the format yyyy-mm-dd.");
> formObj.HousekeepingRequest.focus();
> return false;
> }
....
Second, I don't copy out all values into variables unless I'm going to
to lots of manipulations, so for the formObj.TVVCR element (and others
like it) I'd something like
if ( ! inRange( formObj.TVVCR,1,1,"TV/VCR") ) {
return false;
}
where inRange( elem, low, high, label) is a function you write to turn
the field into a number and check to see if it is an acceptable value
and handle error conditions.
function inRange( elem, low, high, label) {
var val = Number(elem.value);
if ((val < low) || (val > high)) {
alert("Please enter a value between " + low + " and " + high + "
into the " + labal + " field.");
elem.focus();
return false;
}
return true;
}
On Tuesday, January 28, 2003, at 01:01 PM,
javascript-request@xxxxxxxxxx wrote:
> function ValidateThis(formObj)
> {
> var totalcount = formObj.NumberOfTables.value;
> var tableformat = formObj.TableFormat.value;
> var chairscount = formObj.NumberOfChairs.value;
> var chairsformat = formObj.ChairsFormat.value;
> var housekeepingrequest = formObj.HousekeepingRequest.checked;
> var hrd = formObj.HousekeepingRequestDate.value;
> var tvvcr = formObj.TVVCR.value;
>
> if (totalcount > 1 && tableformat == "Not applicable") {
> alert("You must select a table format when you have requested 2 or
> more
> tables.");
> formObj.TableFormat.focus();
> return false;
> }
>
> else if (chairscount > 1 && chairsformat == "Not applicable") {
> alert("You must select a chairs format when you have requested 2 or
> more
> chairs.");
> formObj.ChairsFormat.focus();
> return false;
> }
> else if ((housekeepingrequest && hrd == "") || (housekeepingrequest &&
> (hrd.length != 10 || hrd.charAt(4) != '-' || hrd.charAt(7) != '-' ||
> (hrd.charAt(5) != 1 && hrd.charAt(5) != 0)))) {
> alert("You have selected housekeeping request. Therefore you must
> include
> a valid housekeeping request date in the format yyyy-mm-dd.");
> formObj.HousekeepingRequest.focus();
> return false;
> }
> else if (tvvcr < 0 || tvvcr > 2) {
> alert ("Invalid quantity of tv/vcr.");
> formObj.TVVCR.focus();
> return false;
>
> }
> else return true;
> }
>
> What I am realizing is that I have another 6 or 7 FORM fields that
> need to
> be included in the data validation that are similar to tvvcr. I
> suppose for
--Apple-Mail-2-1051384419
Content-Transfer-Encoding: 7bit
Content-Type: text/enriched;
charset=US-ASCII
Craig,
Before anything else, search the net for _good_ form validation
scripts. No sense re-inventing the wheel. This will give you more
robust code than if you roll your own.
--------------
First, I don't think you need the else clauses. When a return
statement is executed the function terminates. So, maybe something
like this
<excerpt>function ValidateThis(formObj) {
var totalcount = formObj.NumberOfTables.value;
var tableformat = formObj.TableFormat.value;
var chairscount = formObj.NumberOfChairs.value;
var chairsformat = formObj.ChairsFormat.value;
var housekeepingrequest = formObj.HousekeepingRequest.checked;
var hrd = formObj.HousekeepingRequestDate.value;
if (totalcount > 1 && tableformat == "Not applicable") {
alert("You must select a table format when you have requested 2 or
more tables.");
formObj.TableFormat.focus();
return false;
}
if ((housekeepingrequest && hrd == "") || (housekeepingrequest &&
(hrd.length != 10 || hrd.charAt(4) != '-' || hrd.charAt(7) != '-' ||
(hrd.charAt(5) != 1 && hrd.charAt(5) != 0)))) {
alert("You have selected housekeeping request. Therefore you must
include
a valid housekeeping request date in the format yyyy-mm-dd.");
formObj.HousekeepingRequest.focus();
return false;
}
</excerpt>....
Second, I don't copy out all values into variables unless I'm going
to to lots of manipulations, so for the formObj.TVVCR element (and
others like it) I'd something like
if ( ! inRange( formObj.TVVCR,1,1,"TV/VCR") ) {
return false;
}
where inRange( elem, low, high, label) is a function you write to turn
the field into a number and check to see if it is an acceptable value
and handle error conditions.
function inRange( elem, low, high, label) {
var val = Number(elem.value);
if ((val << low) || (val > high)) {
alert("Please enter a value between " + low + " and " + high + "
into the " + labal + " field.");
elem.focus();
return false;
}
return true;
}
On Tuesday, January 28, 2003, at 01:01 PM,
javascript-request@xxxxxxxxxx wrote:
<excerpt><fixed>function ValidateThis(formObj)
{
var totalcount = formObj.NumberOfTables.value;
var tableformat = formObj.TableFormat.value;
var chairscount = formObj.NumberOfChairs.value;
var chairsformat = formObj.ChairsFormat.value;
var housekeepingrequest = formObj.HousekeepingRequest.checked;
var hrd = formObj.HousekeepingRequestDate.value;
var tvvcr = formObj.TVVCR.value;
if (totalcount > 1 && tableformat == "Not applicable") {
alert("You must select a table format when you have requested 2 or
more
tables.");
formObj.TableFormat.focus();
return false;
}
else if (chairscount > 1 && chairsformat == "Not applicable") {
alert("You must select a chairs format when you have requested 2 or
more
chairs.");
formObj.ChairsFormat.focus();
return false;
}
else if ((housekeepingrequest && hrd == "") || (housekeepingrequest &&
(hrd.length != 10 || hrd.charAt(4) != '-' || hrd.charAt(7) != '-' ||
(hrd.charAt(5) != 1 && hrd.charAt(5) != 0)))) {
alert("You have selected housekeeping request. Therefore you must
include
a valid housekeeping request date in the format yyyy-mm-dd.");
formObj.HousekeepingRequest.focus();
return false;
}
else if (tvvcr << 0 || tvvcr > 2) {
alert ("Invalid quantity of tv/vcr.");
formObj.TVVCR.focus();
return false;
}
else return true;
}
What I am realizing is that I have another 6 or 7 FORM fields that
need to
be included in the data validation that are similar to tvvcr. I
suppose for
</fixed></excerpt>
--Apple-Mail-2-1051384419--
- Prev by Date: [Javascript] CSS in JS function - where is the error?
- Next by Date: [Javascript] question
- Previous by thread: [Javascript] CSS in JS function - where is the error?
- Next by thread: [Javascript] compose dynamical forms with JS
- Index(es):