I am trying to write a javascript function which is called input() (performing input validation) and it will contain 3 arguments – the arguments are message, min and max.
I am having trouble bringing/writing the the function as a whole, have managed to work out bits of the function though – any help appreciated!
Code so far:
// declare variables
var min = 5
var max = 20
var message = 'input must be between 5 and 20';
// get user input
input = parseInt(prompt(message));
// check range
while(isNaN(input) || input<min || input>max) {
alert('input was invalid');
input = parseInt(prompt(message));
}
// output validation
alert('input was accepted');
(from http://jsfiddle.net/AnJym/2/)
looks pretty good – the last thing to do is to declare a function and put your code in the “function body”
you could also write
function input(min, max, message) { ... }, but it’s a matter of taste.Now you can do
var i = input(5, 25, "input must been 5 and 25")andiwill contain the validated variable.