JavaScript Validation

Revision as of 15:27, 12 March 2014 by Monika (talk | contribs) (To make a check box field conditionally mandatory)

Revision as of 15:27, 12 March 2014 by Monika (talk | contribs) (To make a check box field conditionally mandatory)

In addition to the built-in Numeric, Alphabetic, Date and Allow Empty validations available on Custom Fields, users can also define their own validation or criteria for a custom field by creating a validation based on JavaScript syntax.

The JavaScript syntax can be entered into the JavaScript Validation field on the custom field settings page.

Contents

To accept only certain values

>n or @value@>n

If @value@ is not found, the system will automatically put it at the very front of the validation criteria.

Where

n is any number

> means greater than

< means less than

>= means greater than or equal to

<= means less than or equal to

== means equal to

!= means Not equal to

Example

To have a custom field only accept values larger than 100, enter

>100 or @value@>100 in the Custom box.

To limit the amount of characters that can be entered into a field

@value@.length<n

Where

n is any number

Example

To limit the number of characters in a field to 35 or less

@value@.length<35

To have a custom field only accept values in a range

@value@>n && @value@<n

Where

n is any number

&& means "AND"

|| means "OR"

! means "Not"

() means brackets :-)

Example

To have a custom field only accept values between 100 and 200, enter

" @value@>99 && @value@<201 "

To perform pattern matching

isMatch(@value@, pattern, isEmptyOK)

Where pattern is

L = Letter

N = Number

A = Alphanumeric

AND

isEmptyOK is

True or False

Example

To validate 9 digits SIN number, put this into the box

isMatch(@value@, 'NNNNNNNNN', false)

To allow only numbers and one other character

isFloat(@value@.replace(/c/g,""),true)

where

c is any character

Example

To allow a field to contain only numbers and a semi-colon

isFloat(@value@.replace(/;/g,""),true)

To allow letters and a space

isAlphabetic(@value@,Allow Empty,Allow Space)

where

Allow Empty - true/false

Allow Space - true/false

Example

To allow a field to contain only letters and a space

isAlphabetic(@value@,true,true)

To allow only integers

parseInt(@value@)==@value@

To check if a phone number of a certain format

isPhoneStr(@value@, "XXX-XXX-XXXX")

To validate proper Email Address

isEmail(@value@)

To validate against a different custom field

You can setup two fields (Amount Requested, Project Budget), and use the following to syntax to check the value entered on one of them against the other. Put this on the Amount Requested field to check if requested is more than the project budget

frm.cf_@Project Budget.id@.value>=@value@


To validate against a standard field

You can perform validation against standard fields as well. For example, to validate that the a Canadian postal code syntax should only be enforced for Canadian contacts, use:

document.frmuser.ucountry.value!="10" || isMatch(@value@, 'LNL NLN', true)

The syntax is:

document.entity.standard field name.value

where:

  • entity is frmuser for contact records and frmcompany for company records and
  • standard field name is the standard field name (see Variable List for field names)

To make a field conditionally mandatory

If you wish to make a field mandatory only if the value Yes is selected from a dropdown field, for example, use:

@value@!="" || frm.cf_fieldid.value!="Yes"

in the JavaScript validation setting where:

When using this method, ensure that "Allow Empty" is selected for this field.

To make a check box field conditionally mandatory

If there is a check box field under a show hide header and you wish to make this check box field mandatory only if the value Yes is selected from a dropdown field, Paste JavaScript below in the JavaScript Validation field on the "Select Many - Check Boxes" field type:
isChkSelected(frm.cf_fieldid) || frm.cf_fieldid.value!="Yes".

in the JavaScript validation syntax where:

1) isChkSelected(frm.cf_fieldid)= this (frm.cf_fieldid) is the field id of the check box field to be made conditionally mandatory.
2) frm.cf_fieldid.value= this cf_fieldid is the custom field ID of the dropdown field.

For example: Combo box field- Did your organization receive any non-monetary assistance? (1387274) Options are Yes;No; If yes is selected than they should answer a mandatory check box field. Non Monetary Assistance chk box (1378251).

Put this JavaScript in JavaScript Validation field:

isChkSelected(frm.cf_1378251) || frm.cf_1387274.value!="Yes"



When using this method, ensure that "Allow Empty" is selected for this field.

See Also