How can I redirect to another path in JavaScript if a user inputs a non-numeric value for a paginated URL parameter?

18 views Asked by At

How to determine the type of a character wrapped in quotes?

I am retrieving params from a URL that needs to be evaluated through a type test to be used in a pagination service: http://192.168.1.8:4200/posts?page=g

I want to redirect to another path if user puts anything other than number as a page param

I have tried using if(typeof JSON.parse(queries.page)===number)

it works in case of a number input, but does not get through in case of a string for example:

http://192.168.1.8:4200/posts?page=g

it generates an error of:

VM873:1 Uncaught SyntaxError: Unexpected token 'g', "g" is not valid JSON at JSON.parse () at :1:14

1

There are 1 answers

0
Mahmoud Mirzaei On

The answer is using isNaN as an identifier:

I can use isNaN() instead of typeof()

if(isNaN(+queries.page))

returns true

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/NaN