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
Restrict alpha/string field
Re: Restrict alpha/string field
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;
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
Thanks Josh, it works perfect.
Re: Restrict alpha/string field
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
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
-
- Posts: 1792
- Joined: December 5th, 2011, 11:27 pm
- Location: Washington, DC
Re: Restrict alpha/string field
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;
if VALUE < 10000 then
errmsg("...");
reenter;
endif;