Integer validation for input
When the reading fails, you set valid
to false
, so the condition in the while
loop is false
and the program returns input
(which is not initialized, by the way).
You also have to empty the buffer before using it again, something like:
#include <iostream>
#include <limits>
using namespace std;
double read_input()
{
double input = -1;
bool valid= false;
do
{
cout << "Enter a number: " << flush;
cin >> input;
if (cin.good())
{
//everything went well, we'll get out of the loop and return the value
valid = true;
}
else
{
//something went wrong, we reset the buffer's state to good
cin.clear();
//and empty it
cin.ignore(numeric_limits<streamsize>::max(),'\n');
cout << "Invalid input; please re-enter." << endl;
}
} while (!valid);
return (input);
}
JavaScript validating input only contains integers on submit
To check if a number is an integer try this. It returns a boolean.
Number.isInteger(yourNumber)
docs: https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/Synchronous_and_Asynchronous_Requests
Validation of integer with character input in C program
Some notes:
1) You aren't checking the scanf()
return value, and this is very usable: the negative return means that the entered characters can't be converted to int
(because of "%d" format), and the return value equal 0 means that the input is empty (no characters entered).
2) In case that the user entered wrong character(s) (not digit(s)), the input buffer will remain busy until you read it in other way. Good idea is to use additional scanf("%s")
here to read any characters as string, so buffer will be empty after this call. Using rewind()
is not enough here.
3) There is no need to additional checking of input in selectionChecking()
for isdigit()
, because "%d" format in scanf()
doesn't allow to read anything else but number.
4) There is no need to pass pointer to selection
value in selectionChecking()
call - it will be enough to pass it as value.
So, try this below:
// declaration of 'selectionCheck()'
int selectionCheck(int input, int min, int max);
void main()
{
int selection;
while (1)
{
while (1)
{
printf("Enter Your Selection (0-4) > ");
int ret = scanf("%d", &selection);
if (ret < 0) // invalid characters on input
{
printf("Invalid characters\n");
scanf("%s"); // empty buffer, reading it as string and putting readed characters to nowhere ;)
continue; // go to top of loop
}
if (ret == 0) // empty input
{
printf("No (empty) input\n");
continue; // go to top of loop
}
// here 'ret' is greather than 0, so valid number was entered
if (selectionCheck(selection, 0, 4)) // is value between 0 and 4 ?
break; // yes, success, break current loop!
printf("Invalid value\n");
}
printf("Success\n");
}
system("pause");
}
int selectionCheck(int input, int min, int max)
{
if (input < min || input > max)
return 0;
else
return 1;
}
Of course, you can write 'selectionCheck()' more condensed:
int selectionCheck(int input, int min, int max)
{
return (input < min || input > max) ? 0 : 1;
}
or simply:
int selectionCheck(int input, int min, int max)
{
return (input >= min && input <= max);
}
Javascript validation for input type number
You can prevent user from entering more than 1 .
and -
using e.preventDefault()
in keydown event.
let input = document.querySelector('input');
input.addEventListener('keydown',(e) => {
if((e.key === '.' && e.target.value.includes('.')) || e.key === '-'){
e.preventDefault();
console.log("Wrong Input")
}
})
input.addEventListener('blur',(e) => {
if(parseFloat(e.target.value) < 0.5) console.log("Input less than 0.5")
})
<input id="main" type="number" min="0.5"/>
angular/forms integer validator
You can try Angular Reactive form pattern validator
this.savingData = this.formBuilder.group({
amount: ['', [Validators.required, Validators.pattern("^[0-9]*$")]], // only numbers
});
Related Topics
Why Does Poll Keep Returning Although There Is No Input
Gmon.Out Is Not Written After Compiling with Gcc -Pg -G
Overriding a Base's Overloaded Function in C++
How Do Objects Work in X86 at the Assembly Level
What Is Indeterminate Behavior in C++ ? How Is It Different from Undefined Behavior
How to Compile Qt 5 Under Windows or Linux, 32 or 64 Bit, Static or Dynamic on Visual Studio or G++
Difference Between *(Pointer + Index) and Pointer[]
Why Does Long Long N = 2000*2000*2000*2000; Overflow
"Undefined Reference To" Using 'G++' to Compile a C++ Program
Are There Any Way to Link My Program with Wine-Compiled Part
How to Make a Function Async-Signal-Safe
How to Get Memory Information on Linux System
Is There a Difference Between Universal References and Forwarding References