Field JavaScript Validation (RCS code)

Mindwatering Incorporated

Author: Tripp W Black

Created: 01/06/2006 at 03:43 PM

 

Category:
Notes Developer Tips
JavaScript

Text / Text Area / Select-one / Select-multiple Field Validation:
Updated RCS Code for Form JS Validation Including Select-Multiple Type:

rcs_javascript_validation_sample.txt

Thanks to Marc of
Rocking Chair Software:
http://www.rockingchairsoftware.com

Addition to RCS and MW code to retrieve the JavaScript Control RTF field contents:
if(CheckAllFields() == true) {
GetJSEditorData(); // used with the JS digi JavaScript Control RTF field
_getEditAppletData(); // used with java applet RTF field
//_doClick('$Refresh', this, null); // uncomment to do refresh instead of submit
f.submit();
}

Addition to RCS and MW code to confirm field exists is below. Add it just below the function start.
function CheckField(field, msg) {
if (field=== undefined) {
// field is missing, abort check
return true;
}



Addition to RCS and MW code that verifies existence of field before validation for the clear field and check field validation functions. Needed to catch if undefined so that JS doesn't blow up on a missing, misspelled, or expectably hidden by hide-when.
ClearFldStyleCheckFieldUpdated.rtfClearFldStyleCheckFieldUpdated.rtf

Addition to RCS code that verifies a file upload control has something in its Browse field.
(It does not verify the text is legit if the user hand entered something.)

CheckFileUpload.txt

Additional function to check the value (and Text) of a drop-down selection for validation comparisons:
CheckFldVal.txtCheckFldVal.txt


Radio Button Field Validation:
MW Addition to RCS code to check a Radio Button group:
CheckButton.txtButtonWhichOptionSelected.txt ButtonWhichOptionSelected.txt

MW Addition to set or check a radio button group:
SetButtonValue.rtfSetButtonValue.rtf


Checkbox Field Validation:
MW Addition to RCS code to check a set of Checkboxes to make sure that at least one of the check boxes are checked.

checkbox_validation_function.txtIsACheckBoxFieldChecked.txt

Better version of checkbox_validation_function.txt that can also handle a single/lone checkbox:
checkbox_with_singlebox_validation.txt checkbox_with_singlebox_validation.txt

MW Addition to RCS code to check if a specific checkbox (value) has been checked.
IsCheckBoxValueMatchedPassedValue.txt IsCheckBoxValueMatchedPassedValue.txt

MW Addition to RCS code to check if any list box item has been selected.
CheckListField.rtfCheckListField.rtf


E-mail and Phone Number (dashed: 123-123-1234) Validation:
Validates that an e-mail has a format such as:
jane.doe@mydomain.com
jdoe@mydomain.com.au
Validates phone number for fixed dashed format of 111-222-3333.

EmailAddressPhoneNumber.txt EmailAddressPhoneNumber.txt


Number Field Validation:
Validates that a field contains ONLY numbers (e.g. Positive integer whole numbers).

CheckNumberField.txtCheckNumberField.txt


Validate Field Value Starts With a Matching String - @Begins Equivalent:

function CheckFldBegins(field, chkstr, msg) {
  var curstr;
  if (field.type=="text" | field.type=="textarea") {
          curstr = field.value;
		if ( curstr.startsWith( chkstr )  ) {
		    field.className="";
		    return true;
		} else {
		    // failure
			field.className="error";
			errorMsgs += msg + "\n\r";
			field.focus();
			return false;
		}
	}
	// not correct type, return true
	field.className="";
	return true;
} 



Validate Two Type=Date Fields that First Date Field is Before the Second Date:

...
	if(!CheckFldDates(fld1, fld2, "Date Start must be before or the same as the Date End date", 1)) hasErrors = true;
...

function CheckFldDates(fld1, fld2, msg, okSame) {
    if ( ! fld1) {
        // field does not exist
        console.log('(CheckFldDates) fld1 is null; failed check.');
        return false;
    }
    if ( ! fld2) {
        // field does not exist
        console.log('(CheckFldDates) fld2 is null; failed check.');
        return false;
    }
    var dt1v = fld1.value;
    var dt2v = fld2.value;
    if ( dt1v == '') {
        // field has no value
        console.log('(CheckFldDates) fld1 is empty; failed check.');
        errorMsgs += msg + "\n\r";
        fld1.className = "valerror";
        return false;
    }
    if ( dt2v == '') {
        // field has no value
        console.log('(CheckFldDates) fld2 is empty; failed check.');
        errorMsgs += msg + "\n\r";
        fld2.className = "valerror";
        return false;
    }
    var dt1 = new Date(dt1v).getTime();
    var dt2 = new Date(dt2v).getTime();
    if (dt1 < dt2) {
         fld1.className = "";
         fld2.className = "";
        return true;
    }
    if (dt1 == dt2) {
        if (okSame == 1) {
            fld1.className = "";
            fld2.className = "";
            return true;
        } else {
            console.log('(CheckFldDates) fld1 and fld2 are same and same is not allowed.');
            errorMsgs += msg + "\n\r";
            fld1.className = "valerror";
            fld2.className = "valerror";
            return false;
        }
    }
    // dt1 > dt2
    console.log('(CheckFldDates) fld1 >  fld2 and is not allowed.');
    errorMsgs += msg + "\n\r";
    fld1.className = "valerror";
    fld2.className = "valerror";
    return false;
}



Validate Two Type=Time Fields that First Time Field is Before the Second Time:



...
     if(!CheckFldTimes(fld1, fld2, "End Time must be at least 1 hour after begin Time", 3600)) hasErrors = true;
...

function CheckFldTimes(fld1, fld2, msg, okSecs) {
    if ( ! fld1) {
        // field does not exist
        console.log('(CheckFldTimes) fld1 is null; failed check.');
        return false;
    }
    if ( ! fld2) {
        // field does not exist
        console.log('(CheckFldTimes) fld2 is null; failed check.');
        return false;
    }
    if ( okSecs == '') {
        okSecs = 0;
    }
    var tm1v = fld1.value;
    var tm2v = fld2.value;
    if ( tm1v == '') {
        // field has no value
        console.log('(CheckFldTimes) fld1 is empty; failed check.');
        errorMsgs += msg + "\n\r";
        fld1.className = "valerror";
        return false;
    }
    if ( tm2v == '') {
        // field has no value
        console.log('(CheckFldTimes) fld2 is empty; failed check.');
        errorMsgs += msg + "\n\r";
        fld2.className = "valerror";
        return false;
    }
    var timeStartSplit = tm1v.split(':');
    var timeEndSplit = tm2v.split(':');
    var startDtTm = new Date(0, 0, 0, timeStartSplit[0], timeStartSplit[1], 0);
    var endDtTm = new Date(0, 0, 0, timeEndSplit[0], timeEndSplit[1], 0);
    var diff = endDtTm.getTime() - startDtTm.getTime();
    if (diff/1000 > okSecs || diff/1000 == okSecs) {
         fld1.className = "";
         fld2.className = "";
        return true;
    } 
    // fld1 > fld2
    console.log('(CheckFldTimes) fld1 >  fld2 and is not allowed.');
    errorMsgs += msg + "\n\r";
    fld1.className = "valerror";
    fld2.className = "valerror";
    return false;
}



previous page

×