0

I am trying to check that the entry in to one of my forms only contains digits. I have found a method , isdigit(), however this is only applicable to strings. I have tried a number of things, but nothing seems to work:

request.form.get("units").str().isdigit() == True
str(request.form.get("units")).isdigit() == True
request.form.get("units").isdigit() == True

It seems like it should not be hard, so feel like there's something obvious I do not understand.

Thanks!

3
  • request.form.get("units") should be a string already. Have you tried printing this to the console, to see whether the string you receive consists of digits only? like units = request.form.get("units"); print('"{}" {}'.format(units, units.isdigit())) (the "" within the string to show where the units string begins and ends in case it contains whitespace) – Blauelf Oct 4 '17 at 9:11
  • Well it is a user input field, so I'd know if it contains digits or not because I'd be the one inputting them. I guess my question is what is the syntax to use the isdigit method to check this form only contains digits? – rover11235 Oct 4 '17 at 18:10
  • How does "but nothing seems to work" manifest? For example, if if not request.form.get("units").isdigit(): return apology("Must enter number of shares") does not work and the error is like AttributeError: 'NoneType' object has no attribute 'isdigit', this would mean that request.form.get("units") is None (because there's no form field of name "units" in the request). – Blauelf Oct 5 '17 at 9:52

You must log in to answer this question.

Browse other questions tagged .