IsValid

Description

Tests whether a value meets a validation or data type rule.

Returns

True, if the value conforms to the rule; False, otherwise.

Function syntax

IsValid(type, value) 
isValid("range", value, min, max)  
isValid("regex" or "regular_expression", value, pattern)

History

ColdFusion 8: Added the component value for to the type attribute.

ColdFusion MX 7: Added this function.

Parameters

Parameter

Description

type

The valid format for the data; one of the following. For detailed information on validation algorithms, see Validating form data using hidden fields in the Developing ColdFusion Applications.

  • any: any simple value. Returns false for complex values, such as query objects;; equivalent to the IsSimpleValue function.

  • array: an ColdFusion array; equivalent to the IsArray function.

  • binary: a binary value;; equivalent to the IsBinary function.

  • boolean: a Boolean value: yes, no, true, false, or a number; equivalent to the IsBoolean function.

  • component: a ColdFusion component (CFC).

  • creditcard: a 13-16 digit number conforming to the mod10 algorithm.

  • date or time: any date-time value, including dates or times;; equivalent to the IsDate function..

  • email: a valid email address.

  • eurodate: any date-time value, including US date formats and time values,

  • float or numeric: a numeric value; equivalent to the IsNumeric function.

  • guid: a Universally Unique Identifier of the form "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" where ‘ X’ is a hexadecimal number.

  • integer: an integer.

  • query: a query object; equivalent to the IsQuery function.

  • range: a numeric range, specified by the min and max attributes.

  • regex or regular_expression: matches input against pattern attribute.

  • ssn or social_security_number: A U.S. social security number.

  • string: a string value, including single characters and numbers

  • struct: a structure; equivalent to the IsStruct function.

  • telephone: a standard US telephone number.

  • URL: an http, https, ftp, file, mailto, or news URL,

  • UUID: a ColdFusion Universally Unique Identifier, formatted ‘ XXXXXXXX-XXXX-XXXX-XXXXXXXXXXXXXXX’, where ‘ X’ is a hexadecimal number. See CreateUUID.

  • USdate: a U.S. date of the format mm/dd/yy, with 1-2 digit days and months, 1-4 digit years.

  • variableName: a string formatted according to ColdFusion variable naming conventions.

  • zipcode: U.S., 5- or 9-digit format ZIP codes.

  • maxlength: Specifies a maximum number of characters

value

The value to test

min

The minimum valid value; used only for range validation

max

The maximum valid value; used only for range validation

pattern

A JavaScript regular expression that the parameter must match; used only for regex or regular_expression validation.

Usage

The IsValid function lets you assure that validation is performed on the server. You can use the cfparam tag to perform equivalent validation.

Example

The following example checks whether a user has submitted a numeric ID and a valid e-mail address and phone number. If any of the submitted values does not meet the validation test, it displays an error message.

<cfif isDefined("form.saveSubmit")> 
    <cfif isValid("integer", form.UserID) and isValid("email", form.emailAddr)  
            and isValid("telephone", form.phoneNo)> 
        <cfoutput> 
            <!--- Application code to update the database goes here ---> 
            <h3>The email address and phone number for user #Form.UserID#  
                have been added</h3> 
        </cfoutput> 
    <cfelse> 
        <H3>You must supply a valid User ID, phone number, and email address.</H2> 
    </cfif> 
    <cfelse> 
</cfif> 
 
<cfform action="#CGI.SCRIPT_NAME#"> 
    User ID:<cfinput type="Text" name="UserID"><br> 
    Phone: <cfinput type="Text" name="phoneNo"><br> 
    email: <cfinput type="Text" name="emailAddr"><br> 
    <cfinput type="submit" name="saveSubmit" value="Save Data"><br> 
</cfform>