CFLib.org – Common Function Library Project

CheckPattern(checkWhat, str)

Last updated January 15, 2003

Version: 2 | Requires: ColdFusion 5 | Library: StrLib

 
Rated 0 time(s). Average Rating: 0

Description:
RPV uses regular expressions to check and match the string to a predefined pattern. For instance, date, phone number, credit card and so on. 2 parameters are required for using RPV, name of the pattern to check (see code), and the string to validate. If a patter is not found, the UDF will return false. Using UDF, you can extend the functionality by including your own patterns. Just add another case towards the bottom (above the default). Usage: #CheckPattern(PatternToCheck,stringToCheck)# Note: Many of these patterns are off of the internet. In the next version, I'll try to include as many credits to the original authors as possible, including specific example of pattern matches and non-matches. I can really only take credit for the UDF and not the patterns.

Return Values:
Returns a boolean.

Example:

view plain print about
<cfif CheckPattern("isIP","10.10.10.10")>
The IP you supplied will be ...
<cfelse>
Please provide a valid IP address
</cfif>

Parameters:

Name Description Required
checkWhat Name of the pattern to use. Yes
str String to check. Yes

Full UDF Source:

view plain print about
<cfscript>
/**
 * This UDF is an extensible, easy to use pattern validator using regular expressions.
 * Rewrites by rcamden.
 * 
 * @param checkWhat      Name of the pattern to use. (Required)
 * @param str      String to check. (Required)
 * @return Returns a boolean. 
 * @author Chris Chay (itadept@earthlink.net) 
 * @version 2, January 15, 2003 
 */

function CheckPattern(checkWhat, str) {
    var rePattern=""// Assign RE pattern to this variable
    switch (checkWhat){
        case "isEmail":
            rePattern="^([\w\d\-\.]+)@{1}(([\w\d\-]{1,67})|([\w\d\-]+\.[\w\d\-]{1,67}))\.(([a-zA-Z\d]{2,4})(\.[a-zA-Z\d]{2})?)$";
            break;
        case "isIP":
            rePattern="^(((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|0?[0-9]?[0-9])\.){3,3}(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|0?[0-9]?[0-9]))$";
            break;
        case "isFloat":
            rePattern="^[-+]?\d*\.?\d*$";
            break;
        case "isInteger":
            rePattern="^[+-]?\d+$";
            break;
        case "isUSPhone":
            rePattern="^((\(\d{3}\) ?)|(\d{3}-))?\d{3}-\d{4}$";
            break;
        case "isUSCurrency":
            rePattern="^\$(\d{1,3}(\,\d{3})*|(\d+))(\.\d{2})?$";
            break;
        case "isDate":
            rePattern="^(?:(?:(?:0?[13578]|1[02])(\/|-|\.)31)\1|(?:(?:0?[1,3-9]|1[0-2])(\/|-|\.)(?:29|30)\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})$|^(?:0?2(\/|-|\.)29\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))$|^(?:(?:0?[1-9])|(?:1[0-2]))(\/|-|\.)(?:0?[1-9]|1\d|2[0-8])\4(?:(?:1[6-9]|[2-9]\d)?\d{2})$";
            break;
        case "isCreditCard":
            rePattern="^((4\d{3})|(5[1-5]\d{2})|(6011))-?\d{4}-?\d{4}-?\d{4}|3[4,7]\d{13}$";
            break;
        case "isSSN":
            rePattern="^\d{3}-\d{2}-\d{4}$";
            break;
        case "isZipCode":
            rePattern="^\d{5}-\d{4}|\d{5}|[A-Z]\d[A-Z] \d[A-Z]\d$";
            break;
        default:
            return("That pattern check is not available");
            break;
    }
    return reFindNoCase(rePattern,str);
}
</cfscript>
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Tayo Akinmade Tayo Akinmade added
arrayTrim
11 day(s) ago

Will Belden Will Belden added
longTime
17 day(s) ago

James Sleeman James Sleeman added
quickSort
27 day(s) ago

Ben Forta Ben Forta added
GetHostAddress
29 day(s) ago

Top Rated

Darwan Leonardo Sitepu EksporSQLData
Rated 5.0, 16 time(s)

Darwan Leonardo Sitepu backupDatabase
Rated 5.0, 13 time(s)

Barney Boisvert indentXml
Rated 5.0, 10 time(s)

Kevin Pepperman generateSsccAsn
Rated 5.0, 4 time(s)

Created by Raymond Camden / Design by Justin Johnson