
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Accept Only Numbers Between 0 to 255 in JavaScript
JavaScript is a popular scripting language used to create interactive websites and applications. It has many powerful features that make it easy to use and extendable.
This can be useful for validating user input on forms or other data-driven tasks where you want to ensure the user enters a value within a specific range.
In this article, we will discuss how to accept only numbers between 0 and 255 range using JavaScript.
The range function in JavaScript returns a list of all the integers after receiving the beginning and ending indexes. The difference between the lowest and highest numbers is what it symbolizes. Sorting all the numbers between the starting and ending positions is made easier with the range function.
Using if else condition in JavaScript
If a certain condition is true, the if/else statement causes a block of code to run. Another block of code may be run if the condition is false. The if/else statement is a part of JavaScript's "Conditional" Statements, which are used to carry out various operations based on various conditions.
Syntax
Following is the syntax for ?if else' condition
if (condition){ } else { }
Example
In the following example, we are running a script by using the ?if else' condition to check whether the given numbers are in range or not.
<!DOCTYPE HTML> <html> <body> <script> function checkRangeDemo(number) { if (number < 0 || number > 255) { return false; } else { return true; } } var number = 55; if (checkRangeDemo(number) == true) document.write(number + " is in range"+"<br>") else document.write(number + " is not in range") var number1 = 350; if (checkRangeDemo(number1) == true) document.write(number1 + " is in range") else document.write(number1 + " is not in range") </script> </body> </html>
When the script is run, it will generate an output, triggering the event, and check that the given values are within the range. In our case, it will display true in the first iteration and false in the second iteration.
Example
Let's consider another example, where we are using the and operator (&&) along with the ?if' condition to check whether the given number is within the range or not.
<!DOCTYPE html> <html> <body> <script> const num = 69; const low = 0; const high = 255; if (num > low && num < high) { document.write('True'); } else { document.write('False'); } </script> </body> </html>
When the user runs the script, it will display an output, making the event active, and checks whether the given values lie in the range or not, and give the value. In our case, it will display "true" on the web-browser.
Let's look into another example to understand more about how to set values to accept numbers between 0 and 255.
Example
Considering the following example, where we are running a condition to check whether the given values satisfy the condition or not.
<!DOCTYPE HTML> <html> <body> <script> var checkRange = function (num) { var num = Number(num); return !isNaN(num) && (num % 1 === 0) && num >= 0 && num < 255; }; document.write(checkRange(33)+"<br>"); document.write(checkRange(369)); </script> </body> </html>
On running the above script, the event gets triggered, and checks the given values in the script, which return true for the first value and false for the second value.