[Zope] How to check if a string contains some characters??

Duncan Booth duncan@rcp.co.uk
Tue, 13 Mar 2001 11:03:12 +0000


Marcello asked:

> Is there a simple way to check if a string contain only this values:
> "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789.-_"
> and otherwise raise an error?
>
Why have all the responses so far been so excessively complex? This looks 
like an ideal candidate for string.translate.

Input: <dtml-var input missing="not set"><br>
<dtml-if input>
<dtml-let valid="_.string.letters+_.string.digits + '.-_'"
    badchars="_.string.translate(input, _.string.maketrans(' ', ' '), 
valid)">
<dtml-if badchars> Sorry, you aren't allowed those characters (<dtml-var 
badchars html_quote>)
<dtml-else>Nice string
</dtml-if>
</dtml-let>
</dtml-if>