2

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 ??

1
  • You should use a Django form with an IntegerField, then it will validate this for you. Commented Jun 10, 2015 at 6:35

2 Answers 2

2

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
Sign up to request clarification or add additional context in comments.

Comments

0

You can try :

 if int(re.search(r'\d+', anytext).group())):
    #perform action

1 Comment

This will throw an exception if anytext doesn't have a numeric value. You could try if re.search(r'\d+', anytext): instead.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.