Page 1 of 1

Make alphanumeric questions compulsory

Posted: September 5th, 2022, 2:59 pm
by wimneel
Alphanumeric questions, by default, can be left blank. What is the simplest way to change that behaviour, i.e. make them compulsory?
Thanks for your help.

Re: Make alphanumeric questions compulsory

Posted: September 5th, 2022, 10:01 pm
by sherrell
There is no setting to do this, you will have to add logic to ensure the string isn't empty.

Code: Select all

if strip(myString) <> "" then	// the string isn't blank
// continue with processing the string
endif;
or

Code: Select all

if length(strip(myString)) > 0 then	// the string length isn't zero
// continue with processing the string
endif;
Sherrell

Re: Make alphanumeric questions compulsory

Posted: September 6th, 2022, 4:45 am
by wimneel
Thank you very much, Sherrell.
Wouldn't it be nice to have this as a feature, e.g. a property of the question, with a default that can be set to either compulsory or not?

Re: Make alphanumeric questions compulsory

Posted: September 13th, 2022, 2:49 am
by JeffCid
I have a similar issue.
I have several Alhpanumeric fields that I want want to make inputs compulsory. I am new to coding so I just copied the codes above and pasted in one of the fields logic section. I did replace "mystring" with Structure_Code ( Name of the variable) and compiled. But the field still accepts zeo inputs.
Any help please? Maybe I did something wrong.

Re: Make alphanumeric questions compulsory

Posted: September 13th, 2022, 11:51 am
by sherrell
The logic needs to be in the postproc of the variable you are testing. Alternatively, you could make this a function and call the function for all string variables so you don't have to repeat the logic. If your code isn't working, please paste the entire code block (starting from PROC VarName).

As for making this a CSPro setting, I agree this could be useful--tho how would that work? If we say don't allow blank, then invariably we'd want to stipulate the string length (for example, having a person's name field be 1 character probably doesn't make sense). You would then likely want to stipulate whether only alphabetic characters could be entered, or a mix of alphabetic & numeric characters, etc. Any thoughts?

Thanks,
Sherrell

Re: Make alphanumeric questions compulsory

Posted: September 13th, 2022, 10:29 pm
by joshua.deguito
JeffCid wrote: September 13th, 2022, 2:49 am I have a similar issue.
I have several Alhpanumeric fields that I want want to make inputs compulsory. I am new to coding so I just copied the codes above and pasted in one of the fields logic section. I did replace "mystring" with Structure_Code ( Name of the variable) and compiled. But the field still accepts zeo inputs.
Any help please? Maybe I did something wrong.
Hi JeffCid,

I use this code for my alphanumeric fields.

Code: Select all

	if strip($) = "" then	// if the string is blank
		errmsg("You can't leave this question blank"); 
		reenter;

	elseif $ = "None" then  // if the respondent answered None or Refused
		//continue...
	endif;

Re: Make alphanumeric questions compulsory

Posted: September 14th, 2022, 1:37 am
by JeffCid
Thanks a lot ! This code is working. One quick one
How do I run this code once on a number of fields at once?

Re: Make alphanumeric questions compulsory

Posted: September 14th, 2022, 9:18 am
by Gregory Martin
You can write a function and then call it in every location where you want to made this check. For example:
function EnsureNotBlank(string value, numeric min_length = 1)

    if length(strip(value)) < min_length then
        errmsg
("You must enter text of length %d+ for field %s", min_length, getsymbol());
        reenter;
    endif;
   
end;

PROC AAA

    EnsureNotBlank($);

PROC BBB

    EnsureNotBlank($, 5);

Re: Make alphanumeric questions compulsory

Posted: September 14th, 2022, 9:24 am
by JeffCid
Sure, thanks!