Feeds:
Posts
Comments

Match regex is a great addition to 4Dv11, but what I really need is a function that takes a string value, a data type and tells me if it’s valid or not.

I’m no regex expert, just compiled some online examples into one function. This is only a starting point, but it completes my needs for the time being. There is a whole forum section devoted to this online.

  ` ----------------------------------------------------
  ` User name (OS):
  ` Date and time: 11/02/10, 15:29:39
  ` ----------------------------------------------------
  ` Method: UTIL_ValidateDataByType
  ` Description
  ` We want to have a method that will validate data by it's type
  `
  ` Parameters
  ` ----------------------------------------------------
  `$0 returns true or false based on validity
  `$1 text data to validate cast as String
  `$2 longint type of data expected
  `$3 boolean show alerts (optional) assumed false

C_BOOLEAN($0;$vb_Return;$3;$vb_ShowAlerts)
C_TEXT($1;$vt_Data;$vt_Pattern)
C_LONGINT($2;$vl_DataType;$vl_ParameterCount)

  ` count the parameters
$vl_ParameterCount:=Count parameters
  ` assume we'll fail
$vb_Return:=False
  ` assume we don't want to see alerts
$vb_ShowAlerts:=False

If ($vl_ParameterCount>=2)
	  ` lets check them
	$vt_Data:=$1
	$vl_DataType:=$2
	
	If ($vl_ParameterCount>=3)
		$vb_ShowAlerts:=$3
	End if 
	
	Case of 
		: ($vl_DataType=Is Alpha Field ) | ($vl_DataType=Is Text )
			  ` we passed in a string didn't we?
			$vb_Return:=True
		: ($vl_DataType=Is Boolean )
			
			$vt_Data:=Lowercase($vt_Data)
			
			Case of 
				: (($vt_Data="true") | ($vt_Data="false"))
					  ` expecting "true" or "false"
					$vb_Return:=True
					
				: ($vb_ShowAlerts)
					ALERT("Expecting 'True' or 'False' not "+$vt_Data)
			End case 
			
		: ($vl_DataType=Is Integer ) | ($vl_DataType=Is LongInt )
			
			  ` integer only, no size restriction
			$vt_Pattern:="[-+]?\\b\\d+\\b"
			
			Case of 
				: (Match regex($vt_Pattern;$vt_Data))
					$vb_Return:=True
					
				: ($vb_ShowAlerts)
					ALERT("Expecting signed integer not "+$vt_Data)
			End case 
			
		: (($vl_DataType=Is Real ) | ($vl_DataType=Is Float ))
			
			  `decimal 
			$vt_Pattern:="[-+]?((\\b[0-9]+)?\\.)?[0-9]+\\b"
			
			Case of 
				: (Match regex($vt_Pattern;$vt_Data))
					$vb_Return:=True
					
				: ($vb_ShowAlerts)
					ALERT("Expecting decimal value not "+$vt_Data)
			End case 
			
		: ($vl_DataType=Is Date )
			  ` Date d/m/yy and dd/mm/yyyy 1/1/00 through 31/12/99 and 01/01/1900 through 31/12/2099
			  ` ie europeon or american style dates accepted, deliminated with '-' ' ' '/' OR '.'
			$vt_Pattern:="\\b(0?[1-9]|[12][0-9]|3[01])[- /.](0?[1-9]|1[012])[- /.](19|20)?[0-9]{2}\\b"
			
			Case of 
				: (Match regex($vt_Pattern;$vt_Data;1))
					$vb_Return:=True
					
				: ($vb_ShowAlerts)
					ALERT("Expecting d/m/yy or dd/mm/yyyy not "+$vt_Data)
			End case 
			
		: ($vl_DataType=Is Time )
			
			  ` Time in 24 hours format with optional seconds
			$vt_Pattern:="^(([0-1]?[0-9])|([2][0-3])):([0-5]?[0-9])(:([0-5]?[0-9]))?$"
			
			Case of 
				: (Match regex($vt_Pattern;$vt_Data))
					$vb_Return:=True
					
				: ($vb_ShowAlerts)
					ALERT("Expecting HH:MM or HH:MM:SS in 24hr format not "+$vt_Data)
			End case 
			
		Else 
			  ` not checking blobs, pictures, subtables. just move on
			$vb_Return:=True
	End case 
	
Else 
	$vb_Return:=False
End if 

$0:=$vb_Return

Key modifiers can mean lot for contextual clicks:

Want to know if the user has the shift key down: Shift down

How about caps lock: Caps lock down

But want to know if a control key is down?

http://doc.4d.com/4D-Language-Reference-12/User-Interface/Windows-Ctrl-down.301-155201.en.html returns true for windows and the command (Macintosh command down) key on mac

Is it just me or should anything with Windows in the command only return true for windows machines? I would like to either see a generic Is command or control down or split the functionality of os specific functions as to not cause confusion.

I feel the same way about Macintosh option down and Windows alt down.

Instead of throwing an error when GOTO RECORD goes outside the range of record numbers in a table lets have a command that validates the action first.

Note: RECORDS IN SELECTION(table) does not necessarily equal the valid range of record numbers.

Design a site like this with WordPress.com
Get started