Can anyone tell me what are the alternatives to validate user input? this is just a very basic code I wrote today.
- there is a button coded in HTML
- when the user click on it, it will prompt for input
- the input is only numbers
var a, b;
function cal(){
a = prompt("enter the width");
while(isNaN(a) || a <= 0 || a > 1000){
alert("Invalid Input, enter it again!");
return cal();
}
b = prompt("enter the length");
while(isNaN(b) || b <= 0 || b > 1000){
alert("Invalid Input, enter it again!");
return cal();
}
result(a,b);
}
function result(a,b){
var perimeter = (2*a)+(2*b);
var area = (a*b);
document.write("Perimeter: "+perimeter+"<br>");
document.write("Area: "+area+"<br>");
}