Page 1 of 1

Restrict alpha/string field

Posted: June 7th, 2016, 7:40 am
by Alpha
Please, I have an alpha field field that has a length of 15, i would like to restrict the characters that one can enter into
the field. Example address of the household for all clusters will contain the following characters " G,F,D,/ , 1,2,3,4,5,6,7,8,9,0".

Interviewers should be restricted to this characters. I tried the poschar function but i was not able to achieve the restriction.

Thanks in advance

Re: Restrict alpha/string field

Posted: June 8th, 2016, 6:57 am
by josh
You can loop through the characters in the field and check each character to see if it is one of the allowed characters. Something like:
PROC MYALPHA

string validChars =  "GFD/1234567890";

// loop through characters in string one by one from first character
// to last non-blank character
do numeric i = 1 while i <= length(strip(MYALPHA))

    
// Get character at position i
    string char = MYALPHA[i:1];

   
// Check if this character is valid by checking its position in the string of valid characters.
   // A position of zero means it is not in the string so it is invalid.
    if pos(char, validChars) = 0 then
        
errmsg("Invalid character '%s'. Only the following characters are allowed: %s", char, validChars);
        
reenter;
   
endif;
enddo;

Re: Restrict alpha/string field

Posted: June 8th, 2016, 10:01 am
by Anysia
Thanks Josh, it works perfect.

Re: Restrict alpha/string field

Posted: May 18th, 2017, 9:21 am
by pcpak
Hi Josh,
Can this be done on numeric field?
Suppose I have a numeric field of 5 digits and I want to populate a message when user enter less than 5 digits.
thanks

Re: Restrict alpha/string field

Posted: May 18th, 2017, 11:58 am
by Gregory Martin
For a numeric field, you could add a value set to the field, allowing codes 10000-99999. Or you could write:
PROC VALUE

    if VALUE < 10000 then
        errmsg("...");
        reenter;
    endif;