[Javascript] Re: form validation
Roger Roelofs
rer at datacompusa.com
Tue Jan 28 13:44:53 CST 2003
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 at LaTech.edu 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
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: text/enriched
Size: 3602 bytes
Desc: not available
URL: <http://lists.evolt.org/pipermail/javascript/attachments/20030128/37bdd255/attachment.bin>
More information about the Javascript
mailing list