Hi,
Is it possible to restrict an alphanumeric question to strings only, while allowing accents (for french surveys) ?
Thanks,
Hi,
Is it possible to restrict an alphanumeric question to strings only, while allowing accents (for french surveys) ?
Thanks,
There may be other, more elegant ways, but here’s a solution I see:
// accept alphanumeric characters
// see more here: https://docs.mysurvey.solutions/syntax-guide/questions/syntax-guide-text-questions/#IsAlphaLatin
my_text_var.IsAlphaLatin()
||
// or French accents
// see more here: https://docs.mysurvey.solutions/syntax-guide/questions/syntax-guide-text-questions/#ConsistsOf
my_text_var.ConsistsOf("ÀÂÄÉÈÊËÎÏÔÖÙÛÜàâäéèêëîïôöùûü")
Alternatively, one could use regular expressions. There are more elegant ways to designate sets of characters or permit case-insensitive matching, but I’m writing it in long-form for clarity:
// check whether `my_text_var` matches the regex mattern in quotes
// in this case, contains 1+ of the characters in brackets
System.Text.RegularExpressions.Regex.IsMatch(
my_text_var,
// letters A-Z, a-z, numbers, or French accents
"[A-Za-z0-9ÀÂÄÉÈÊËÎÏÔÖÙÛÜàâäéèêëîïôöùûü ]+"
)
See relevant C# documentation here.
Others will hopefully have better suggestions.
Hm, the first solution doesn’t work because in a text with ordinary letters and accented letters both expressions will be false.
Regular expressions will work, but you have to include more letters like ô and ç, etc.
A better expression would be
“^\p{L}+$”
which allows any Unicode character with the ‘letter’ property.
This will also work for spanish, portuguese, german, etc. (e.g. ñ, ã, ä, ß, etc.)
The full validation expression is:
System.Text.RegularExpressions.Regex.IsMatch(self, “^\p{L}+$”)
Just to add on Klaus great suggestion:
If I see it correctly, to use it in Survey Solutions you’d need to use two backslashes for escaping sequence, so the expression would be:
`
System.Text.RegularExpressions.Regex.IsMatch(self, "^\\p{L}+$")
`
That’s correct and it is exactly what I pasted into my comment.
Now I see that the paste operation removes (interprets?) one of the backslashes?!
Interesting.
Thank you all for your very helpful comments! It worked!
See here:
and here:
https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/tokens/verbatim
This is not related to regular expressions. You can try with a string variable.
t="\t"
Then display on the screen the value of %t%
.
Best, Sergiy Radyakin