Javascript implements a numeric addition function, while javascript numeric Addition

Source: Internet
Author: User

Javascript implements a numeric addition function, while javascript numeric Addition

If you don't talk nonsense, simply provide the code.

JS

<Script type = "text/javascript"> function Sum (arg1, arg2) {// numeric addition function var sarg1 = new String (arg1 ); // convert the input parameter to a String for parameter check var sarg2 = new String (arg2); // convert parameter 2 to the character type if (sarg1 = "") | (sarg2 = "") // ensure that the parameter is not empty {var e0 = new Error (); // If a parameter is null, an exception e0.Serial = 1000001 is thrown; // error code if (sarg1 = "") // enter the error message {e0.message = "the Sum function parameter is invalid: the first parameter is null! "; // Error description} else {e0.message =" the Sum function parameter is invalid: 2nd parameters are blank! ";} Throw e0; // throw error message} for (I = 0; I <sarg1.length; I ++) {// check the validity of the parameter for (j = 0; j <10; j ++) {// check all characters if (sarg1.charAt (I) = j) // if not a number, an error message {break is thrown; // skip loop} else {if (j = 9) // when the number 9 has been found {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 if (sarg2.charAt (k) from 0 to 9 one by one) = l) // if it is 0 ~ 9 number {break;} else {if (l = 9) // an Error message {var e2 = new Error () is thrown only when a non-number is contained (); // create the error object e2.Serial = 1000001; // The 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 and} function buttonequalonclick () are returned () {// standalone 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 to calculate Text3.value = sum; // output the calculation result} catch (e) {// if an error occurs, alert (e. message); // output the information in the exception if (e. serail = 1000001) // if it is error 1000001 {alert (e. message); e = null ;}}</script>

HTML:

<Input type = "text" id = "Text1" style = "width: 84px "maxlength =" 20 "/> + <input type =" text "id =" Text2 "style =" width: 75px "maxlength =" 20 "/>=< input type =" text "id =" Text3 "style =" width: 69px "> <input type =" button "id =" Button1 "value =" Calculate "onclick =" return Button1_onclick () "/>

The above is all the content of this article. I hope you will like it.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.