1

I want to restrict the string id to (a-z), (A-Z) and underscore. I replace underscores by 'a' before testing then run string.isalnum(). The function continues to work even when the user enters an id with non-alphanumerical value. This is the code:

def execute(id):
    test=id
    test=test.replace("_","a")
    if not test.isalnum:
        report("id is not valid, id must be (a-z,A-Z, _)")
        return
    #id is valid, continue working
    #the problem is that it continues working even if for example id ='location?#!!$%'
moamen
  • 169
  • 1
  • 7
  • 1
    _What_ is string#isalnum? (eg. method, property, field, other?) Yes, this matters on how to correctly use it! – user2864740 Jun 14 '20 at 20:44
  • 2
    You probably need `test.isalnum()` instead of `test.isalnum`. The latter is just built-in method, and is doing nothing to check `test`. – Prateek Dewan Jun 14 '20 at 20:44
  • I didn't see that! Thank you, I wonder how that code didn't throw an error – moamen Jun 14 '20 at 20:49
  • 1
    It didn’t throw an error because “not” accepts _any_ expression/value, and test.isalnum evaluated to the function-object (as opposed to the result of invoking such). Since this function-object is a “truthy” value, “not truthy” evaluates to False, and the “if” logic never ran - https://stackoverflow.com/q/39983695/2864740 – user2864740 Jun 14 '20 at 21:12

0 Answers0