How To Verify If An Input Box Accepts Only Numbers Through Selenium

Hello Guys,

You should not be able to type alphabets or special characters in a field which supposed to accept only numbers. Manually you can type alphabets or special characters and verify. But in automation it is little tricky.

Your application developer might restrict alphabets and special characters by setting value of “type” attribute of input tag as “number”. In this case automation will be easy.

Before we move ahead, I would like to say when type attribute of an input box is set as “number”, it works as below:

As per W3:

A string is a valid floating-point number if it consists of:

  1. Optionally, a U+002D HYPHEN-MINUS character (-).
  2. One or both of the following, in the given order:
    1. A series of one or more ASCII digits i.e. 0-9 .
    2. Both of the following, in the given order:
      1. A single U+002E FULL STOP character (.).
      2. A series of one or more ASCII digits.
  3. Optionally:
    1. Either a U+0065 LATIN SMALL LETTER E character (e) or a U+0045 LATIN CAPITAL LETTER E character (E).
    2. Optionally, a U+002D HYPHEN-MINUS character (-) or U+002B PLUS SIGN character (+).
    3. A series of one or more ASCII digits.

So, alphabets ‘E’ and ‘e’ are acceptable alphabets. While ‘+’, ‘-‘ and  ‘.’ are acceptable special characters. So, 1.23E-11 or 1.23E+11 or 2e5 are valid values.

In above case, we can automatically test functionality in two ways:

  1. Type alphabets and special characters ( except acceptable as above ) and retrieve typed text. It should be empty.
  2. Get the “type” attribute of input tag and assert if it is a “number”.


Enter a number : <input type="number" name="quantity">

Java Code:


Alphabets are not allowed.
Special characters are not allowed.
Only valid numbers are allowed.

But if your application is developed by putting validation after typing by user, above ways will not work.

If you have any doubt, feel free to comment below.
If you like my posts, please like, comment, share and subscribe.

Author: Amod Mahajan

My name is Amod Mahajan and I am an IT employee with 6+ years of experience in Software testing and staying in Bengaluru. My area of interest is Automation testing. I started from basics and went through so many selenium tutorials. Thanks to Mukesh Otwani as his tutorials are easy and cover basics to advance. I have habit of exploring concepts by deep diving. I used to make notes. I thought of sharing my knowledge through posts and now I am here. #KeepLearning #ShareLearning