Make alphanumeric questions compulsory

Discussions about CSEntry
Forum rules
New release: CSPro 8.0
Post Reply
wimneel
Posts: 86
Joined: May 6th, 2017, 5:31 am

Make alphanumeric questions compulsory

Post 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.
sherrell
Posts: 397
Joined: April 2nd, 2014, 9:16 pm
Location: Washington, DC

Re: Make alphanumeric questions compulsory

Post 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
wimneel
Posts: 86
Joined: May 6th, 2017, 5:31 am

Re: Make alphanumeric questions compulsory

Post 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?
JeffCid
Posts: 13
Joined: September 13th, 2022, 2:04 am
Location: Ghana

Re: Make alphanumeric questions compulsory

Post 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.
sherrell
Posts: 397
Joined: April 2nd, 2014, 9:16 pm
Location: Washington, DC

Re: Make alphanumeric questions compulsory

Post 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
joshua.deguito
Posts: 20
Joined: August 17th, 2022, 11:07 pm

Re: Make alphanumeric questions compulsory

Post 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;
JeffCid
Posts: 13
Joined: September 13th, 2022, 2:04 am
Location: Ghana

Re: Make alphanumeric questions compulsory

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

Re: Make alphanumeric questions compulsory

Post 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);
JeffCid
Posts: 13
Joined: September 13th, 2022, 2:04 am
Location: Ghana

Re: Make alphanumeric questions compulsory

Post by JeffCid »

Sure, thanks!
Post Reply