I want to test whether a request parameter is greater then 12 or not JavaScript.
It always gives the alert “lesser” even when I change the value of q to 12.
The current URL is: www.test.com?id=2
How can this be done in JavaScript?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Your code works. If it returns true, then it alerts greater. Although you should be adding semicolons at the end of statements. Your code would look like this:
Working demo
As said by Derek, if q === 10, then it would alert ‘Lesser’ because 10 is not more than 10.
EDIT: Here, you have 2 options. Either use something like PHP to make q equal to the number like this:
Note that using this would open your site up to all kinds of XSS, so if you decide to go this way, learn a bit about security, and don’t rush into uploading it to a live website.
Or, if you want to to do it using JavaScript only, then do it like this:
The 10 is the radix parameter, avoiding you getting strange numbers for inputs such as 010 (would be 8 without the parameter).