| <Script type = "text/javascript"> Function Sum (arg1, arg2) {// numeric addition function Var sarg1 = new String (arg1); // convert the input parameter into a String for parameter check Var sarg2 = new String (arg2); // convert parameter 2 to character type If (sarg1 = "") | (sarg2 = "") // make sure the parameter is not empty { Var e0 = new Error (); // If a parameter is null, an exception is thrown. E0.Serial = 1000001; // error code If (sarg1 = "") // enter the correct error message based on the null Parameter { The e0.message = "Sum function parameter is invalid: the first parameter is null! "; // Error description } Else { The e0.message = "Sum function parameter is invalid: the first parameter is null! "; } Throw e0; // throw an error message } For (I = 0; I <sarg1.length; I ++) {// check the parameter Validity For (j = 0; j <10; j ++) {// check all characters If (sarg1.charAt (I) = j) // if it is not a number, an error message is thrown. { Break; // skip Loop } Else { If (j = 9) // when the number 9 has been queried { Var e1 = new Error (); // Error message object E1.Serial = 1000001; // error code E1.message = "Sum function parameter:" + sarg1 + "is an invalid number! "; // Error description Throw e1; } } } } For (k = 0; k <sarg2.length; k ++) // check that parameter 2 is a number { For (l = 0; l <10; l ++) {// compare one by one from 0 to 9 If (sarg2.charAt (k) = l) // if it is 0 ~ 9 digits { Break; } Else { If (l = 9) // only contains non-numbers, an error message is thrown. { Var e2 = new Error (); // create an Error object E2.Serial = 1000001; // error code E2.message = "Sum function parameter:" + sarg2 + "is an invalid number! "; Throw e2; } } } } Return Number (arg1) + Number (arg2); // if both parameters are correct, two values are returned. } Function Button1_onclick () {// single-host event handler for the "compute" button Try { Var Text1 = document. getElementById ("Text1 "); Var Text2 = document. getElementById ("Text2 "); Var Text3 = document. getElementById ("Text3 "); Var sum = Sum (Text1.value, Text2.value); // call the function for Calculation Text3.value = sum; // output the calculation result } Catch (e) {// output error message if an error occurs Alert (e. message); // output information in the exception If (e. Serail = 1000001) // if it is error 1000001 { Alert (e. message ); E = null; } } } </Script> |