Throws error in case of invalid data : throw « Language Basics « JavaScript DHTML






Throws error in case of invalid data

  


<html>
<head>
<title>Try/Catch</title>
</head>
<body>
<form name="formexample" id="formexample" action="#">
<div id="valueDiv">Enter a value: <input id="value" name="value"></div>
<div><input id="submit" type="submit"></div>
</form>
<script type="text/javascript">
function checkValid() {
    try {
        var valueField = document.forms[0]["value"];
        if (valueField.value != "A") {
            throw "It's not what I need";
        }
    }
    catch(errorObject) {
        alert(errorObject);
    }
}
function init() {
    document.forms[0].onsubmit = function() { return checkValid() };
}
window.onload = init;
</script>
</body>
</html>

   
    
  








Related examples in the same category

1.Catch that error
2.Throw that error
3.Exception handling with try/catch