Indigo Rose Software

Professional Software Development Tools

 
Results 1 to 3 of 3
  1. #1
    Join Date
    Feb 2001
    Location
    Indigo Rose Software
    Posts
    2,728

    Post Function: Validate the Contents of an Input Object

    This function can be used in AutoPlay Media Studio 5.0 Professional to perform some simple data validation on an input object. You can use it to test the object's contents for the following criteria:
    • minimum length (at least x characters)
    • maximum length (no more than x characters)
    • numeric characters only (no letters allowed)
    This function also demonstrates a naming technique that lets you create "categorized" functions that resemble the built-in actions in AutoPlay. You simply create a table and then define the function as an item in that table. (This is in fact how the built-in actions are done, although with some extra behind-the-scenes autocomplete and quickhelp magic.)

    Code:
    -- create the InputTest table if it doesn't already exist
    if not InputTest then InputTest = {}; end
    
    --[[	
    Function:	InputTest.Validate
    Purpose:	Perform some simple validation on an input object.
    Arguments:	(string) ObjectName - The name of the object you want to test.
              	(number) nMinChars - The minimum number of characters required.
              	(number) nMaxChars - The maximum number of characters allowed. (nil for no limit)
              	(boolean) bNumbersOnly - true if only numeric characters are allowed.
    Returns:	true if the edit field is meets the criteria, false if it doesn't.
    --]]
    function InputTest.Validate(ObjectName, nMinChars, nMaxChars, bNumbersOnly)
    	local strText = Input.GetText(ObjectName);
    	local nLen = String.Length(strText);
    	
    	if(nLen < nMinChars) then
    		return false;
    	end
    	
    	if( (nMaxChars ~= nil) and (nLen > nMaxChars)) then
    		return false;
    	end
    
    	if(bNumbersOnly) then
    		-- does the string consist only of valid numeric characters?
    		if(String.TrimRight(strText, "1234567890-.") ~= "") then
    			return false;
    		end
    	end
    
    	return true;
    end
    Here is a short example demonstrating how you might call the function. Insert this example code in the On Click event for an object on a page that has an input object named "Input1" on it.

    Code:
    if(InputTest.Validate("Input1", 3, 5, true)) then
    	Dialog.Message("HK-47 says:","Observation: Well done, Master! Your response passes my expert validation protocols.");
    else
    	Dialog.Message("HK-47 says:","Warning: Incorrect response.\r\n\r\nResolution: Enter a number between 3 and 5 digits long.");
    end
    Note that the function's second and third parameters are optional...you can test for only a minimum of 3 characters (with no maximum and alphanumeric characters okay) with the following call:

    Code:
    if(InputTest.Validate("Input1", 3)) then
    	-- test passed
    end
    Requirements:

    Note: this function will only work with AutoPlay Media Studio 5.0 Professional Edition, since the Standard Edition does not have an input object.
    --[[ Indigo Rose Software Developer ]]

  2. #2
    Join Date
    Feb 2001
    Location
    Indigo Rose Software
    Posts
    2,728
    Here is a modified version of the InputTest.Validate function that let's you specify what characters are valid, to make the function more flexible:

    Code:
    -- create the InputTest table if it doesn't already exist
    if not InputTest then InputTest = {}; end
    
    --[[	
    Function:	InputTest.Validate
    Purpose:	Perform some simple validation on an input object.
    Arguments:	(string) ObjectName - The name of the object you want to test.
              	(number) nMinChars - The minimum number of characters required.
              	(number) nMaxChars - The maximum number of characters allowed. (nil for no limit)
              	(string) strValidChars - a string containing the list of valid characters, or nil if no character checking should be performed.
    Returns:	true if the edit field is meets the criteria, false if it doesn't.
    --]]
    function InputTest.Validate(ObjectName, nMinChars, nMaxChars, strValidChars)
    	local strText = Input.GetText(ObjectName);
    	local nLen = String.Length(strText);
    	
    	if(nLen < nMinChars) then
    		return false;
    	end
    	
    	if( (nMaxChars ~= nil) and (nLen > nMaxChars)) then
    		return false;
    	end
    
    	if(strValidChars) then
    		if(String.TrimRight(strText, strValidChars) ~= "") then
    			return false;
    		end
    	end
    
    	return true;
    end
    _strNumericChars = "1234567890-.";
    Instead of a boolean switch to test for numeric characters, you can now pass a list of characters that are valid. The _strNumericChars variable still makes it easy to test for numeric characters. Here's the same example as before, modified to use the new function:

    Code:
    if(InputTest.Validate("Input1", 3, 5, _strNumericChars)) then
    	Dialog.Message("HK-47 says:","Observation: Well done, Master! Your response passes my expert validation protocols.");
    else
    	Dialog.Message("HK-47 says:","Warning: Incorrect response.\r\n\r\nResolution: Enter a number between 3 and 5 digits long.");
    end
    This version of the InputTest.Validate function would work well with a function that determines the appropriate list of characters for the user's locale, e.g. whether the decimal place should be a comma or a period:

    Code:
    --[[	
    Function:	InputTest.GetLocalizedNumericChars
    Purpose:	Get a string containing the valid numeric characters for the current locale settings.
    Arguments:	None.
    Returns:	A string containing the valid numeric characters for the current locale settings.
    --]]
    function InputTest.GetLocalizedNumericChars()
    	local strNumericChars = Registry.GetValue(HKEY_CURRENT_USER,"Control Panel\\International","sMonDecimalSep")
    	                      ..Registry.GetValue(HKEY_CURRENT_USER,"Control Panel\\International","sPositiveSign")
    	                      ..Registry.GetValue(HKEY_CURRENT_USER,"Control Panel\\International","sNegativeSign")
    	                      ..Registry.GetValue(HKEY_CURRENT_USER,"Control Panel\\International","sNativeDigits");
    	
    	return strNumericChars;
    end
    Here is the same example rewritten to use this function:

    Code:
    if(InputTest.Validate("Input1", 3, 5, InputTest.GetLocalizedNumericChars())) then
    	Dialog.Message("HK-47 says:","Observation: Well done, Master! Your response passes my expert validation protocols.");
    else
    	Dialog.Message("HK-47 says:","Warning: Incorrect response.\r\n\r\nResolution: Enter a number between 3 and 5 digits long.");
    end
    --[[ Indigo Rose Software Developer ]]

  3. #3
    Join Date
    Oct 2001
    Location
    Norway
    Posts
    939
    Lorne,
    Your first function was great, but now even GREATER!

    To take advantage of the function InputTest.GetLocalizedNumericChars() I had to add the line
    _strNumericChars = InputTest.GetLocalizedNumericChars();
    AFTER the function’s end statement.

    The code lines in “Globals” OUTSIDE function-end paragraph seem to be executed like “PRE.On Preload” statements?

    In a previous post Lorne’s two great input validation scripts you said that the sign has to be the first character. I removed the "sPositiveSign" and "sNegativeSign" from function InputTest.GetLocalizedNumericChars() and created another function InputTest.GetLocalizedNumericSigns() with these registry values to assign value to _strNumericSigns. At start of function InputTest.Validate(ObjectName, nMinChars, nMaxChars, strValidChars) I added:
    Code:
    if String.Find(_strNumericSigns, String.Left(strText, 1), 1, false) > 0 then
        	-- first character is the sign, remove it
        	strText = String.Mid(strText, 2, nLen - 1);
    end
    International users (with comma as decimappoint) has to remember: If the input value is to be used in a math operation, the comma has to be replaced by the dot.
    Code:
    NumericValue1 = String.Replace(Input.GetText("Input1"), ",", ".", false);

Similar Threads

  1. Function: Test Whether An Input Object Is Empty
    By Lorne in forum AutoPlay Media Studio 5.0 Examples
    Replies: 0
    Last Post: 06-08-2004, 02:18 PM
  2. INFO: Difference between the Media Player Object and the AVI Object
    By Support in forum AutoPlay Media Studio 4.0 Examples
    Replies: 0
    Last Post: 10-29-2002, 02:15 PM
  3. HOWTO: Make a Media Player Object Go Full Screen
    By Support in forum AutoPlay Media Studio 4.0 Examples
    Replies: 0
    Last Post: 10-23-2002, 11:23 AM
  4. Replies: 0
    Last Post: 10-04-2002, 10:09 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts