Input validation for textfield for integers AND specific letter

Hello everybody,

In our webapp, we have field which should accept only integers or letter e (Shorthand for 2e which translates to 2*10).

I would like to evaluate the input, so if user types only:
e - it will not pass (if this validation would be too complicated, it can be omitted)
12 - (int) it will pass
12e - (int + e) it will pass
12ab - it will not pass

How can I do that?
I was able to do only

${is_type}=      Evaluate     isinstance($variable, int)   

But that works only with pure integers. How can I make it works, so that it accepts integer + this specific letter of alphabet (not all of them)?

Hi,

I’m not sure to understand the need.
The goal is to Input Text these values and verify the field accepts it?
How do you check the Pass or Fail of the value? Field has a specific border, raises an error?

If you have the value, maybe a Regex evaluation could be easier:

 Should Match Regexp   ${variable}    ^\d+e?$

Charlie

3 Likes

The isinstance function accepts several types. You should try:

${is_type}=      Evaluate     isinstance($variable, (int, float))

Note, the list of types is a Tuple.

2 Likes

Hi,

This specific field does not raise error, I wanted to prevent bad usage of a keyword, but after brief discussion with a colleague I realized it would be way too complex.

I like the simplicity of your solution and it might come in handy (I am sure it will) in future keywords, so I´ll mark it as a solution.

Thanks for the reply,

Ha, good to know :slight_smile: However, that would not solve my problem with that one specific letter, unless I can just pass it literary?

I misread your original problem. For the case of base ten exponencial numbers, the solution would be using regular expressions as Charlie did propose.

But mathematically, you can do a comparison of the value with its integer part. For example:

  ${is_type}=      Evaluate     $variable == int($variable) and not isinstance($variable, float)

I did not confirmed the code, but you should have an “unit test” for this :slight_smile:

1 Like