Restrict alpha/string field

Discussions about CSEntry
Post Reply
Alpha

Restrict alpha/string field

Post 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
josh
Posts: 2399
Joined: May 5th, 2014, 12:49 pm
Location: Washington DC

Re: Restrict alpha/string field

Post 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;
Anysia
Posts: 28
Joined: March 12th, 2016, 6:54 am

Re: Restrict alpha/string field

Post by Anysia »

Thanks Josh, it works perfect.
pcpak
Posts: 19
Joined: September 8th, 2016, 3:45 am

Re: Restrict alpha/string field

Post 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
Gregory Martin
Posts: 1792
Joined: December 5th, 2011, 11:27 pm
Location: Washington, DC

Re: Restrict alpha/string field

Post 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;
Post Reply