User Guide Cancel

IsValid

 

Description

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

Returns

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

Category

Decision functions

Function syntax

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

See also

cfparam cfform IsBooleanIsDateIsNumericIsSimpleValueValidating data with the IsValid function and the cfparam tag in the Developing ColdFusion Applications

History

ColdFusion (2016 release) Update 3: Added datetime_object as type.

ColdFusion 11: Behavioral change

Prior to ColdFusion 11,  this function allowed currency symbols at the start and commas inside the number. Starting from ColdFusion 11, this function evaluates on a more strict basis. To revert to the old behavior use the application-level setting STRICTNUMBERVALIDATION to false. 

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

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.

  • array: a ColdFusion array; equivalent to the IsArray function.
  • any: any data type.
  • 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 IsNumericDate function.
  • datetime_object: any valid ColdFusion date/time object.
  • email: a valid email address. You can also pass IPV6 format values. Supports RFC standards for validating an enail address. For more information, see Valid email addresses. If it is a valid email address, isValid returns True.
  • eurodate : any date-time value, including US date formats and time values,
  • float: a floating point value.
  • function: any UDF.
  • 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 parameters.
  • regex or regular_expression: matches input against pattern parameter.
  • 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. You can also pass IPV6 format values.
  • 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.
  • xml: any xml object.
  • zipcode: U.S., 5- or 9-digit format ZIP codes.

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>
<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>
<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>

In ColdFusion 11, the isValid function behaves in a different way. Setting strictnumbervalidation to false makes the isValid function to behave in a way just like in the previous versions (ColdFusion 10 or earlier). Starting from ColdFusion 11, a more strict approach will be followed for validation. However,  if you need the isValid function behavior to be of ColdFusion 10, set the key/tag attribute strictnumbervalidation to false in application cfc/cfm.

Application.cfm

 

<cfapplication name="hello" STRICTNUMBERVALIDATION="false" >
<cfapplication name="hello" STRICTNUMBERVALIDATION="false" >
<cfapplication name="hello" STRICTNUMBERVALIDATION="false" >

Application.cfc

<cfcomponent>
<cfscript>
{
this.STRICTNUMBERVALIDATION = false;
}
</cfscript>
</cfcomponent>
<cfcomponent> <cfscript> { this.STRICTNUMBERVALIDATION = false; } </cfscript> </cfcomponent>
<cfcomponent>
<cfscript>
{
this.STRICTNUMBERVALIDATION = false;
}
</cfscript>
</cfcomponent>

Example

 

<cfscript>
// check if 50 is an integer
writeOutput(isValid("integer",50)) // Yes
// check if 3.14 is a numeric
writeOutput(isValid("numeric",3.14)) // Yes
// check if it is a valid url
writeOutput(isValid("URL","http://www.example.com")) // Yes
</cfscript>
<cfscript> // check if 50 is an integer writeOutput(isValid("integer",50)) // Yes // check if 3.14 is a numeric writeOutput(isValid("numeric",3.14)) // Yes // check if it is a valid url writeOutput(isValid("URL","http://www.example.com")) // Yes </cfscript>
<cfscript>
    // check if 50 is an integer
    writeOutput(isValid("integer",50)) // Yes
    // check if 3.14 is a numeric
    writeOutput(isValid("numeric",3.14)) // Yes
    // check if it is a valid url
    writeOutput(isValid("URL","http://www.example.com")) // Yes
</cfscript>

Get help faster and easier

New user?