Tests whether a value meets a validation or data type rule.
True, if the value conforms to the rule; False, otherwise.
IsValid(type, value)
isValid("range", value, min, max)
isValid("regex" or "regular_expression", value, pattern)
cfparam , cfform , IsBoolean, IsDate, IsNumeric, IsSimpleValue; Validating data with the IsValid function and the cfparam tag in the Developing ColdFusion Applications
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.
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.
|
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. |
The IsValid function lets you assure that validation is performed on the server. You can use the cfparam tag to perform equivalent validation.
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>
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" >
Application.cfc
<cfcomponent>
<cfscript>
{
this.STRICTNUMBERVALIDATION = false;
}
</cfscript>
</cfcomponent>
<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>
Sign in to your account