View comments | RSS feed

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)

See also

cfparam, cfform, IsBoolean, IsDate, IsNumeric, IsSimpleValue; Validating data with the IsValid function and the cfparam tag in Validating Data in ColdFusion MX Developer's Guide

History

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 Validating Data in ColdFusion MX Developer's Guide.

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

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

ColdFusion 9 | ColdFusion 8 | ColdFusion MX 7 | ColdFusion MX 6.1 | ColdFusion MX | Forums | Developer Center | KnowledgeBase | Bug Reporting

Version 7

Comments


Jedi Master said on Feb 11, 2005 at 9:00 AM :
When the docs say the regex must be JavaScript regex, they are incorrect. You can use normal CFML regex.
jrunrandy said on Apr 22, 2005 at 12:42 PM :
The 2-argument variant of isValid expects a type and a value. Rather than tell you that "variableName" isn't a data type, the error message is telling you that the variable "404;http://tes.ttest" is not a valid ColdFusion variable name, since it starts with a number. That's my best guess.

In general, you will get a better and quicker answer by posting questions like this to the online forums: http://webforums.macromedia.com/coldfusion or click the Forums link on any CFMX 7 LiveDocs page.
Honest Joe said on Jul 30, 2005 at 12:45 PM :
Careful with INTEGERS - CF does not consider integers to be a subset of numbers - check this out:

isValid(‘Integer’,'500,000') = TRUE
isValid(‘Integer’,'500,0') = TRUE
isValid(‘Integer’,'$500,000') = TRUE
isValid(‘Integer’,'$500,000$') = TRUE

BUT:

isNumeric(‘500,000’) = FALSE
isNumeric(‘500,0’) = FALSE
isNumeric(‘$500,000’) = FALSE
isNumeric(‘$500,000$’) = FALSE
ASandstrom said on Aug 1, 2005 at 7:16 AM :
The IsValid issue has been entered as bug 60818.
tommyviper said on Sep 23, 2005 at 1:34 AM :
It appears IsValid(type, value) should read isValid("type", value)
Baz (thinkLOOP) said on Oct 19, 2005 at 3:58 PM :
Another issue with validating INTEGERS is that they are limited to 4 bytes. So only integers between -2,147,483,648 and 2,147,483,647 actually count as integers. Any integers that are out of this range will return FALSE with an isValid check.

This is consistent with how databases like MySQL or SQLServer define INT but inconsistent with the general defintion of integers or functions such as isNumeric().

If you need to validate BIGINT's in your DB it may be better to stick with good 'ol <cfif round(var) is var>

Examples:

isValid(‘Integer’,'-2147483648') = TRUE
isValid(‘Integer’,'2147483647') = TRUE
isValid(‘Integer’,'-2147483649') = FALSE
isValid(‘Integer’,'2147483648') = FALSE
isValid(‘Integer’,'99999999999999999999999') = FALSE
isValid(‘Integer’,'-99999999999999999999999') = FALSE

BUT:

isNumeric(‘-2147483648') = TRUE
isNumeric(‘2147483647’) = TRUE
isNumeric(‘-2147483649’) = TRUE
isNumeric(‘2147483648’) = TRUE
isNumeric(‘99999999999999999999999’) = TRUE
isNumeric(‘-99999999999999999999999’) = TRUE
Baz (thinkLOOP) said on Oct 20, 2005 at 3:26 PM :
Correction: Using the round() function to validate big integers is not a good solution because the result of rounding any integer over 12 digits long is the scientific notation of that integer.

For example:
Round(9999999999999) = 1E+013

Therefore your if statement would be:
<cfif 1E+013 is 9999999999999>

Which would incorrectly return false.

The best solution may be using RegEx as follows:
isValid("regex", "9999999999999", "^-{0,1}[1-9]+[\d]*")

Happy validating!
MDinowitz_HoF said on Jun 15, 2006 at 4:04 PM :
Note that the RegEx option is case sensitive!
julio5 said on Jul 17, 2006 at 4:10 AM :
Another issue to be aware of with "integer" validation is when passing values in from an index loop. They may look like integers when you type/output them, but CF casts them as floats which will fail the isValid test.

<cfloop index="i" from="1" to="3">
<cfoutput>#isValid("integer",i)#</cfoutput>
</cfloop>

Gives NO NO NO

Against an integer "range", however they pass:

<cfloop index="i" from="1" to="3">
<cfoutput>#isValid("range",i,1,3)#</cfoutput>
</cfloop>

Gives YES YES YES.
cf.Objective said on Sep 22, 2006 at 1:15 PM :
All regexes are case sensitive, hence the need for a-zA-Z, because the ascii code for a is different than the ASCII code for A.

So yeah, it's something to be aware of, I suppose, but it's absolutely typical behavior for any regex tool... whether CF is case-sensitive or not. :)

Laterz...
Paul654321 said on Feb 16, 2007 at 3:22 AM :
This invalidates valid addresses.

<cfset testemail = '"No One" <noOne@NOWhere.com>'>
<cfoutput>#testemail#</cfoutput><br />

<cfif isValid("email",testemail) >
good email
<cfelse>
bad email
</cfif>
Schletz said on Mar 12, 2007 at 11:02 PM :
FYI:
I have noticed that using isValid("email", FORM.email) incorrectly decides that "test@yahoo.com.com" is a valid email address. I had to do a hack to exclude these types of addresses as well.
Le Reve said on May 31, 2007 at 11:48 AM :
Also in reference to integers.

I noticed that when outputting through a query I was not able to use a calculated value evaluate if it was an integer or not until I used the Evaluate function as noted in the example below.

This example illustrates how you might move through a 3 column, 4 row list of items.

This will NOT work because the validation will always return "NO"...

<!---Start my output from a query, since I want 12 item per page I will use from and to even though this would normally be a caluculated value of some sort--->
<cfoutput query="SampleQuery" from="1" to="12">
<!---Set the current row to use in our calculation--->
<cfparam name="ThisRow" default="1">
<!---Output our data using div and float to the left for alignment--->
<div style="float:left;">DISPLAY SOMETHING HERE</div>
<!---Calculate to get our number to validate--->
<cfset ThisRowCalc= SampleQuery.CurrentRow / 3>
<!---Test the number to see if it is an integer and place a breake--->
<cfif IsValid('integer', ThisRowCalc) IS "YES">
<br clear="all">
</cfif>
<!---Set our new number up one step to know which row we are on--->
<cfset ThisRow = ThisRow + 1>
</cfoutput>

If we use the Evaluate function around the calculated value, it will validate the integer correctly and in this case place a <br> every other 3 loops.

This WILL WORK...
<!---Start my output from a query, since I want 12 item per page I will use from and to even though this would normally be a caluculated value of some sort--->
<cfoutput query="SampleQuery" from="1" to="12">
<!---Set the current row to use in our calculation--->
<cfparam name="ThisRow" default="1">
<!---Output our data using div and float to the left for alignment--->
<div style="float:left;">DISPLAY SOMETHING HERE</div>
<!---Calculate to get our number to validate--->
<cfset ThisRowCalc= SampleQuery.CurrentRow / 3>
<!---Test the number to see if it is an integer and place a breake--->
<cfif IsValid('integer', Evaluate(ThisRowCalc)) IS "YES">
<br clear="all">
</cfif>
<!---Set our new number up one step to know which row we are on--->
<cfset ThisRow = ThisRow + 1>
</cfoutput>

NOTE: The only thing that changed was that I used the Evaluate function when validating to see if this row was an integer or not.
halL said on May 31, 2007 at 12:59 PM :
Tested with a post-CF7 release version, but probably true with CF 7 also:
This behavior appears to be true for any result of the division of two integers.
If you replace the query loop with a simple indexed loop and divide the index by 3 you get the same results.
Seraphimalia said on Jun 6, 2007 at 7:14 AM :
In response to Schletz's comment above...

According to RFC 822, test@yahoo.com.com is a perfectly valid email address. What makes this email address invalid is that there is no DNS record for yahoo.com.com.

Coldfusion's IsValid function cannot be excpected to validate an email address based on DNS resolution or VRFY commands with the mail server.
daamsie said on Jun 6, 2007 at 10:48 PM :
The URL validation is also flawed. The following incorrectly considers the URL as invalid, due the colon in the URL.

<cfset testurl = 'http://en.wikipedia.org/wiki/Portal:Netherlands'>
<cfoutput>#testurl#</cfoutput><br />

<cfif isValid("url",testurl) >
good url
<cfelse>
bad url
</cfif>
Seajay said on Feb 13, 2008 at 5:29 AM :
In response to daamsie

A URL which uses the colon (:) symbol as part of the location address is invalid.

The colon is a reserved character according to RFC 1738, and instead, if you are using a colon in the rest of the URL (as opposed to it's normal place after the scheme), you should make sure it is encoded, so your example URL should look like this:

http://en.wikipedia.org/wiki/Portal%3ANetherlands
helpneeded911 said on Mar 21, 2008 at 2:44 AM :
Note that although the "creditcard" description says "a 13-16 digit _number_" - it also accepts strings with spaces or dashes. If a downstream party is more strict about creditcard numbers, accepting _only_ digits, you need to do extra manual validation beyond what IsValid gives you.
helpneeded911 said on Mar 21, 2008 at 2:55 AM :
Seajay: what you said is not quite true. Although RFC 1738 states that "The characters ";", "/", "?", ":", "@", "=" and "&" are the characters which may be reserved for special meaning within a scheme.", it differs per scheme which of those characters are _actually_ reserved.

For HTTP, RFC 1738 specifies:

; HTTP

httpurl = "http://" hostport [ "/" hpath [ "?" search ]]
hpath = hsegment *[ "/" hsegment ]
hsegment = *[ uchar | ";" | ":" | "@" | "&" | "=" ]
search = *[ uchar | ";" | ":" | "@" | "&" | "=" ]

As you can see, the colon is a perfectly valid character in the hsegment part.

 

RSS feed | Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/coldfusion/7/htmldocs/00000534.htm