1

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/)

1

3 Answers 3

2

looks pretty good - the last thing to do is to declare a function and put your code in the "function body"

var input = function(min, max, message) {
    // your code
    return input;
}

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") and i will contain the validated variable.

Sign up to request clarification or add additional context in comments.

Comments

0

Write above three lines in a funnction

function inputvalidation(){
var min = 5;
var max = 20;
var textvalue = document.getElementById('txtinput').value;
if(textvalue < 5 || textvalue > 20) {
  var message = 'input must be between 5 and 20';    
  }
}

call the function on click of a button.

3 Comments

The goal is to write a function that take three arguments
@OttoAllmendinger , Uncle Bob would tell you that it is a pretty bad way to do it: vimeo.com/13439458 =P
hmm...my bad...upvote to ur ans since the author of question got the ans from ur post.
-1
function input(min, max, message){
   var val = document.getElementById('InputControlToBeValidated').value;
   if(val>=min && val<=max){
      return true;
   }
   else{
      alert('input must be between '+ min +' and '+ max);
      return false;
   }
}

I hope this will help you

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.