Alert non number form input in JavaScript
Description
The following code shows how to alert non number form input.
Example
<!DOCTYPE html>
<html>
<body>
<script type="text/javascript">
function myErrorHandler(data){<!--from w w w. ja v a2 s . co m-->
try{
if(data == "string"){
throw "E0";
}else{
throw "E1";
}
}catch(e) {
if(e == "E0"){
return("Error (" + e + "): Entry must be numeric.");
}else{
return("Error (" + e + "): Entry must be numeric.");
}
}
}
function processData(form){
if(isNaN(parseInt(form.myText.value))){
alert(myErrorHandler("string"));
}else{
alert("You have correctly entered a number");
}
}
</script>
<form name="myForm">
Please enter a number:
<input type=TEXT size=10 value="" name="myText">
<input type=BUTTON value="Process" name="myButton" onClick='processData(this.form)'>
</form>
</body>
</html>
The code above generates the following result.