devbox@COMPUTEC The Computec development blog

31Aug/090

Useful RegEx: Check if list contains only integers

Trivial, but useful... For 0 and positive integers only:

^[0-9]+(?:\,[0-9]+)*$

If you wish to include negative integers:

^(?:-?[0-9]+)(?:\,(?:-?[0-9]+))*$

So this might be a useful validity checker function:

<cffunction name="lstContainsIntegersOnly" output="no" returntype="boolean" access="private">
	<cfargument name="lstI" required="yes" type="string">
	<cfargument name="bNegativeAllowed" required="no" type="boolean" default="false">
	<cfscript>
		var bIntegersOnly = FALSE;
		if (arguments.bNegativeAllowed) {
			if (ReFind('^(?:-?[0-9]+)(?:\,(?:-?[0-9]+))*$',arguments.lstI)) { bIntegersOnly = TRUE; }
		} else {
			if (ReFind('^[0-9]+(?:\,[0-9]+)*$',arguments.lstI)) { bIntegersOnly = TRUE; }
		}
		return bIntegersOnly;
	</cfscript>
</cffunction>
Share and Enjoy:
  • Print
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • LinkedIn
  • MisterWong.DE
  • Netvibes
  • Reddit
  • Slashdot
  • StumbleUpon
  • Technorati
  • Twitter
  • Yahoo! Bookmarks
  • LinkArena
  • Live
  • MySpace
  • Yahoo! Buzz
  • Yigg
  • blogmarks
  • Faves
  • FriendFeed
  • MisterWong

Comments (0) Trackbacks (0)

No comments yet.


Leave a comment


No trackbacks yet.