function ereg(strOriginalString, strPattern, varIgnoreCase) ' Function matches pattern, returns true or false ' varIgnoreCase must be TRUE (match is case insensitive) or FALSE (match is case sensitive) dim objRegExp : set objRegExp = new RegExp with objRegExp .Pattern = strPattern .IgnoreCase = varIgnoreCase .Global = True end with ereg = objRegExp.test(strOriginalString) set objRegExp = nothing end function function ereg_replace(strOriginalString, strPattern, strReplacement, varIgnoreCase) ' Function replaces pattern with replacement ' varIgnoreCase must be TRUE (match is case insensitive) or FALSE (match is case sensitive) dim objRegExp : set objRegExp = new RegExp with objRegExp .Pattern = strPattern .IgnoreCase = varIgnoreCase .Global = True end with ereg_replace = objRegExp.replace(strOriginalString, strReplacement) set objRegExp = nothing end function
Tag Archive for expressions
VBScript Regular Expressions
isValidPostCode
<?php
/*
Copyright (c) 2008, reusablecode.blogspot.com; some rights reserved.
This work is licensed under the Creative Commons Attribution License. To view
a copy of this license, visit <a href="http://creativecommons.org/licenses/by/3.0/" >http://creativecommons.org/licenses/by/3.0/</a> or
send a letter to Creative Commons, 559 Nathan Abbott Way, Stanford, California
94305, USA.
*/
// Checks the validity of the format of a supplied UK postcode.
// Completely accurate validation is only possible by attempting to deliver mail to the address, and verifying with the recipient.
// Supports all six common postcode formats, Girobank, Father Christmas, British Forces Post Office, and overseas territories.
function isValidPostCode($postCode)
{
$pattern = "/^([A-PR-UWYZ]([0-9]{1,2}|([A-HK-Y][0-9]|[A-HK-Y][0-9]([0-9]|[ABEHMNPRV-Y]))|[0-9][A-HJKS-UW]) [0-9][ABD-HJLNP-UW-Z]{2}|(GIR 0AA)|(SAN TA1)|(BFPO (C/O )?[0-9]{1,4})|((ASCN|BBND|[BFS]IQQ|PCRN|STHL|TDCU|TKCA) 1ZZ))$/i";
return (preg_match($pattern, trim($postCode)) > 0) ? true : false;
}
?>
isValidPostCode
<%
' Copyright (c) 2008, reusablecode.blogspot.com; some rights reserved.
'
' This work is licensed under the Creative Commons Attribution License. To view
' a copy of this license, visit <a href="http://creativecommons.org/licenses/by/3.0/" >http://creativecommons.org/licenses/by/3.0/</a> or
' send a letter to Creative Commons, 559 Nathan Abbott Way, Stanford, California
' 94305, USA.
' Checks the validity of the format of a supplied UK postcode.
' Completely accurate validation is only possible by attempting to deliver mail to the address, and verifying with the recipient.
' Supports all six common postcode formats, Girobank, Father Christmas, British Forces Post Office, and overseas territories.
function isValidPostCode(postCode)
dim regEx
set regEx = new RegExp
with regEx
.IgnoreCase = true
.Global = true
.Pattern = "^([A-PR-UWYZ]([0-9]{1,2}|([A-HK-Y][0-9]|[A-HK-Y][0-9]([0-9]|[ABEHMNPRV-Y]))|[0-9][A-HJKS-UW]) [0-9][ABD-HJLNP-UW-Z]{2}|(GIR 0AA)|(SAN TA1)|(BFPO (C/O )?[0-9]{1,4})|((ASCN|BBND|[BFS]IQQ|PCRN|STHL|TDCU|TKCA) 1ZZ))$"
end with
if regEx.Test(trim(CStr(postCode))) then
isValidPostCode = true
else
isValidPostCode = false
end if
set regEx = nothing
end function
%>
isValidPostalCode
<%
' Copyright (c) 2008, reusablecode.blogspot.com; some rights reserved.
'
' This work is licensed under the Creative Commons Attribution License. To view
' a copy of this license, visit <a href="http://creativecommons.org/licenses/by/3.0/" >http://creativecommons.org/licenses/by/3.0/</a> or
' send a letter to Creative Commons, 559 Nathan Abbott Way, Stanford, California
' 94305, USA.
' Checks the validity of the supplied postal code, both in general and with regards to the corresponding province.
function isValidPostalCode(postalCode, province)
dim regEx
set regEx = new RegExp
with regEx
.Global = True
.IgnoreCase = True
.Pattern = "^[ABCEGHJ-NPRSTVXY]{1}[0-9]{1}[ABCEGHJ-NPRSTV-Z]{1}[ ]?[0-9]{1}[ABCEGHJ-NPRSTV-Z]{1}[0-9]{1}$"
end with
' Basic check for valid format.
if not regEx.test(postalCode) then
isValidPostalCode = false
exit function
end if
regEx.Pattern = ""
' Check province and select corresponding regular expression pattern.
select case uCase(province)
case "AB"
' Alberta
regEx.Pattern = "^T(0[ABCEGHJ-MPV]|1[ABCGHJ-MPRSV-Y]|2[ABCEGHJ-NPRSTV-Z]|3[ABCEGHJ-NPRZ]|4[ABCEGHJLNPRSTVX]|5[ABCEGHJ-NPRSTV-Z]|6[ABCEGHJ-NPRSTVWX]|7[AENPSVXYZ]|8[ABCEGHLNRSVWX]|9[ACEGHJKMNSVWX])"
case "BC"
' British Columbia
regEx.Pattern = "^V(0[ABCEGHJ-NPRSTVWX]|1[ABCEGHJ-NPRSTV-Z]|2[ABCEGHJ-NPRSTV-Z]|3[ABCEGHJ-NRSTV-Y]|4[ABCEGK-NPRSTVWXZ]|5[ABCEGHJ-NPRSTV-Z]|6[ABCEGHJ-NPRSTV-Z]|7[ABCEGHJ-NPRSTV-Y]|8[ABCGJ-NPRSTV-Z]|9[ABCEGHJ-NPRSTV-Z])"
case "MB"
' Manitoba
regEx.Pattern = "^R(0[ABCEGHJ-M]|1[ABN]|2[CEGHJ-NPRV-Y]|3[ABCEGHJ-NPRSTV-Y]|4[AHJKL]|5[AGH]|6[MW]|7[ABCN]|8[AN]|9[A])"
case "NB"
' New Brunswick
regEx.Pattern = "^E(1[ABCEGHJNVWX]|2[AEGHJ-NPRSV]|3[ABCELNVYZ]|4[ABCEGHJ-NPRSTV-Z]|5[ABCEGHJ-NPRSTV]|6[ABCEGHJKL]|7[ABCEGHJ-NP]|8[ABCEGJ-NPRST]|9[ABCEGH])"
case "NL"
' Newfoundland
regEx.Pattern = "^A(0[ABCEGHJ-NPR]|1[ABCEGHK-NSV-Y]|2[ABHNV]|5[A]|8[A])"
case "NS"
' Nova Scotia
regEx.Pattern = "^B(0[CEHJ-NPRSTVW]|1[ABCEGHJ-NPRSTV-Y]|2[ABCEGHJNRSTV-Z]|3[ABEGHJ-NPRSTVZ]|4[ABCEGHNPRV]|5[A]|6[L]|9[A])"
case "NT"
' North West Territories
regEx.Pattern = "^X(0[XG]|1[A])"
case "NU"
' Nunavut
regEx.Pattern = "^X(0[ABC])"
case "ON"
' Ontario
regEx.Pattern = "^(K(0[ABCEGHJ-M]|1[ABCEG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PRSTV-Z]|5[AB]|7[ABCEGHJ-NPRSTV-Y]|8[NPRSTYZ]|9[ABCEGHJKPRSWX]))|(J(0[ABCEGHJ-NPRSTV-Z]|1[ACEGHJ-NRSTXZ]|2[ABCEGHJ-NRSTWXY]|3[ABEGHLMNPRTVXYZ]|4[BGHJ-NPRSTV-Z]|5[ABCJ-MRTV-Z]|6[AEJKNRSTVWYXZ]|7[ABCEGHJ-NPRTV-Z]|8[ABCEGHLMNPRTVXYZ]|9[ABEHJLNTVXYZ]))"
case "SK"
' Saskatchewan
regEx.Pattern = "^S(0[ACEGHJ-NP]|2[V]|3[N]|4[AHLNPRSTV-Z]|6[HJKVWX]|7[HJ-NPRSTVW]|9[AHVX])"
case "YT"
' Yukon
regEx.Pattern = "^Y(0[AB]|1[A])"
end select
' Test postal code against regular expression pattern.
if regEx.Pattern <> "" and regEx.Test(postalCode) then
isValidPostalCode = true
else
isValidPostalCode = false
end if
end function
%>
isValidZipCode
<?php
/*
Copyright (c) 2008, reusablecode.blogspot.com; some rights reserved.
This work is licensed under the Creative Commons Attribution License. To view
a copy of this license, visit <a href="http://creativecommons.org/licenses/by/3.0/" >http://creativecommons.org/licenses/by/3.0/</a> or
send a letter to Creative Commons, 559 Nathan Abbott Way, Stanford, California
94305, USA.
*/
// Checks the validity of the supplied ZIP code.
function isValidZIPCode($zipCode)
{
return (preg_match("/^[0-9]{5}(-[0-9]{4})?$/i", trim($zipCode)) > 0) ? true : false;
}
?>
isValidZipCode
<%
' Copyright (c) 2008, reusablecode.blogspot.com; some rights reserved.
'
' This work is licensed under the Creative Commons Attribution License. To view
' a copy of this license, visit <a href="http://creativecommons.org/licenses/by/3.0/" >http://creativecommons.org/licenses/by/3.0/</a> or
' send a letter to Creative Commons, 559 Nathan Abbott Way, Stanford, California
' 94305, USA.
' Checks the validity of the supplied ZIP code.
function isValidZIPCode(zipCode)
dim regEx
set regEx = new RegExp
with regEx
.IgnoreCase = True
.Global = True
.Pattern = "^[0-9]{5}(-[0-9]{4})?$"
end with
if regEx.Test(trim(CStr(zipCode))) then
isValidZipCode = True
else
isValidZipCode = False
end if
set regEx = nothing
end function
%>
isValidIP
<?php
/*
Copyright (c) 2008, reusablecode.blogspot.com; some rights reserved.
This work is licensed under the Creative Commons Attribution License. To view
a copy of this license, visit <a href="http://creativecommons.org/licenses/by/3.0/" >http://creativecommons.org/licenses/by/3.0/</a> or
send a letter to Creative Commons, 559 Nathan Abbott Way, Stanford, California
94305, USA.
*/
// Validate an IP address.
function isValidIP($ip)
{
$pattern = "/^([1]?d{1,2}|2[0-4]{1}d{1}|25[0-5]{1})(.([1]?d{1,2}|2[0-4]{1}d{1}|25[0-5]{1})){3}$/";
return (preg_match($pattern, $ip) > 0) ? true : false;
}
?>
isValidIP
<%
' Copyright (c) 2008, reusablecode.blogspot.com; some rights reserved.
'
' This work is licensed under the Creative Commons Attribution License. To view
' a copy of this license, visit <a href="http://creativecommons.org/licenses/by/3.0/" >http://creativecommons.org/licenses/by/3.0/</a> or
' send a letter to Creative Commons, 559 Nathan Abbott Way, Stanford, California
' 94305, USA.
' Validate an IP address.
function isValidIP(ip)
dim regEx
set regEx = new RegExp
with regEx
.IgnoreCase = True
.Global = True
.Pattern = "^([1]?d{1,2}|2[0-4]{1}d{1}|25[0-5]{1})(.([1]?d{1,2}|2[0-4]{1}d{1}|25[0-5]{1})){3}$"
end with
if regEx.Test(trim(CStr(ip))) then
isValidIP = True
else
isValidIP = False
end if
set regEx = nothing
end function
%>
isAlphaNumeric
<?php
/*
Copyright (c) 2008, reusablecode.blogspot.com; some rights reserved.
This work is licensed under the Creative Commons Attribution License. To view
a copy of this license, visit <a href="http://creativecommons.org/licenses/by/3.0/" >http://creativecommons.org/licenses/by/3.0/</a> or
send a letter to Creative Commons, 559 Nathan Abbott Way, Stanford, California
94305, USA.
*/
// Checks if a string contains only alpha characters.
function is_alphanumeric($someString)
{
return (preg_match("/[ws.]/i", $someString) > 0) ? true : false;
}
?>
isAlphaNumeric
<% ' Copyright (c) 2008, <a href="http://www.thecanonman.com;" >www.thecanonman.com;</a> some rights reserved. ' ' This work is licensed under the Creative Commons Attribution License. To view ' a copy of this license, visit <a href="http://creativecommons.org/licenses/by/3.0/" >http://creativecommons.org/licenses/by/3.0/</a> or ' send a letter to Creative Commons, 559 Nathan Abbott Way, Stanford, California ' 94305, USA. ' Checks if a string contains only alphanumeric characters. function isAlphaNumeric(someString) dim regEx set regEx = new RegExp with regEx .Global = true .IgnoreCase = true .Pattern = "[ws.]" end with if regEx.test(someString) then isAlphaNumeric = true else isAlphaNumeric = false end if set regEx = nothing end function %>