I have a form value:
anytext = request.POST.get("my_text", None)
if anytext and anytext.is_string:
perform any action
if anytext and anytext.is_numeric:
perform another action
How can I check for numeric value ??
I have a form value:
anytext = request.POST.get("my_text", None)
if anytext and anytext.is_string:
perform any action
if anytext and anytext.is_numeric:
perform another action
How can I check for numeric value ??
You could use isdigit() assuming anytext is always of type string.
For example:
'Hello World'.isdigit() # returns False
'1234243'.isdigit() # returns True
So with your code:
anytext = request.POST.get("my_text", "")
if anytext.isdigit():
# perform action on numeric string
else:
# perform action on alphanumeric string
You can try :
if int(re.search(r'\d+', anytext).group())):
#perform action
anytext doesn't have a numeric value. You could try if re.search(r'\d+', anytext): instead.