JS Implementation of text box support add and subtract operation method

Source: Internet
Author: User

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en"
"Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
<title> text boxes that support add and subtract operations </title>
<meta http-equiv= "Content-type" content= "text/html;charset=gb2312" >
<style type= "Text/css" >
Body{color: #000; Font-size:14px;font-family:verdana,arial,helvetica,sans-serif, "Song Body";}
Input{border-style:solid;border-color: #868275; Background-color: #f9f9f9; Border-width:1px;color: #505050; Vertical-align:middle;}
</style>
<script language=javascript>
function Floatadd (ARG1,ARG2) {
var r1,r2,m;
Try{r1=arg1.tostring (). Split (".") [1].length}catch (e) {r1=0}
Try{r2=arg2.tostring (). Split (".") [1].length}catch (e) {r2=0}
M=math.pow (10,math.max (R1,R2))
Return (arg1*m+arg2*m)/M
}
function Floatsubtr (ARG1,ARG2) {
Return Floatadd (ARG1,-ARG2);
}
function Ismoney (Money) {
if (/^[\d|+|.| -]+$/.test (Money)) return true;
return false;
}
function result () {
Str=document.getelementbyid ("buy"). Value;
if (! ( Ismoney (str))) {
document.getElementById ("Amount"). InnerHTML = "Invalid Amount";
return false;
}
var len=str.length,money=0,num=0;
var numstr= "", ctrlflag= "X", cflag=0;
for (var i=0;i<len; i++) {
c= Str.charat (i);
if ((c== "+") | | (c== "-")) {
if (I+1<len) {
if ((Str.charat (i+1) = = "+") | | (Str.charat (i+1) = = "-")) Continue
}
if ((numstr!= ") && (IsNaN (NUMSTR))) {
if (IsNaN (NUMSTR)) {
document.getElementById ("Amount"). InnerHTML = "Invalid amount entered";
return false;
}
}
if (numstr!= "") {
Alert ("I:" +i+ "NUMSTR:" +numstr);
if ((ctrlflag== "+") | | (ctrlflag== "x")) {
Num=parsefloat (NUMSTR);
Alert (ctrlflag+ "Money:" +money+ "Num:" +num);
Money=floatadd (Money,num);
Numstr= "";
num=0;
Ctrlflag=c;
}
else{
Alert (money+ "inside minus" +numstr);
Num=parsefloat (NUMSTR);
MONEY=FLOATSUBTR (Money,num);
Numstr= "";
num=0;
Ctrlflag=c;
}
}
else{
Ctrlflag=c;
}
Continue
}
else{
numstr=numstr+ "" +C;
}
}
if ((numstr!= ") && (IsNaN (NUMSTR))) {
if (IsNaN (NUMSTR)) {
document.getElementById ("Amount"). InnerHTML = "Invalid Amount" +NUMSTR;
return false;
}
}
if ((ctrlflag== "-") && (numstr!= "")) {
Num=parsefloat (NUMSTR);
MONEY=FLOATSUBTR (Money,num);
}
if ((ctrlflag== "+") | | (ctrlflag== "x")) && ((numstr!= ""))) {
Num=parsefloat (NUMSTR);
Money=floatadd (Money,num);
}
document.getElementById ("Amount"). InnerHTML =money;
}
</script>
<body>
Please enter add and subtract formula: <input id= "buy" name= "buy" maxlength= "" type= "text" onkeyup= "Javascript:result ();" >
<span id= "Amount" ></span>
</body>

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en"
"Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
<title> text boxes that support add and subtract operations </title>
<meta http-equiv= "Content-type" content= "text/html;charset=gb2312" >
<style type= "Text/css" >
Body{color: #000; Font-size:14px;font-family:verdana,arial,helvetica,sans-serif, "Song Body";}
Input{border-style:solid;border-color: #868275; Background-color: #f9f9f9; Border-width:1px;color: #505050; Vertical-align:middle;}
</style>
<script language=javascript>
function Floatadd (ARG1,ARG2) {
var r1,r2,m;
Try{r1=arg1.tostring (). Split (".") [1].length}catch (e) {r1=0}
Try{r2=arg2.tostring (). Split (".") [1].length}catch (e) {r2=0}
M=math.pow (10,math.max (R1,R2))
Return (arg1*m+arg2*m)/M
}
function Floatsubtr (ARG1,ARG2) {
Return Floatadd (ARG1,-ARG2);
}
function Ismoney (Money) {
if (/^[\d|+|.| -]+$/.test (Money)) return true;
return false;
}
function result () {
Str=document.getelementbyid ("buy"). Value;
if (! ( Ismoney (str))) {
document.getElementById ("Amount"). InnerHTML = "Invalid Amount";
return false;
}
var len=str.length,money=0,num=0;
var numstr= "", ctrlflag= "X", cflag=0;
for (var i=0;i<len; i++) {
c= Str.charat (i);
if ((c== "+") | | (c== "-")) {
if (I+1<len) {
if ((Str.charat (i+1) = = "+") | | (Str.charat (i+1) = = "-")) Continue
}
if ((numstr!= ") && (IsNaN (NUMSTR))) {
if (IsNaN (NUMSTR)) {
document.getElementById ("Amount"). InnerHTML = "Invalid amount entered";
return false;
}
}
if (numstr!= "") {
Alert ("I:" +i+ "NUMSTR:" +numstr);
if ((ctrlflag== "+") | | (ctrlflag== "x")) {
Num=parsefloat (NUMSTR);
Alert (ctrlflag+ "Money:" +money+ "Num:" +num);
Money=floatadd (Money,num);
Numstr= "";
num=0;
Ctrlflag=c;
}
else{
Alert (money+ "inside minus" +numstr);
Num=parsefloat (NUMSTR);
MONEY=FLOATSUBTR (Money,num);
Numstr= "";
num=0;
Ctrlflag=c;
}
}
else{
Ctrlflag=c;
}
Continue
}
else{
numstr=numstr+ "" +C;
}
}
if ((numstr!= ") && (IsNaN (NUMSTR))) {
if (IsNaN (NUMSTR)) {
document.getElementById ("Amount"). InnerHTML = "Invalid Amount" +NUMSTR;
return false;
}
}
if ((ctrlflag== "-") && (numstr!= "")) {
Num=parsefloat (NUMSTR);
MONEY=FLOATSUBTR (Money,num);
}
if ((ctrlflag== "+") | | (ctrlflag== "x")) && ((numstr!= ""))) {
Num=parsefloat (NUMSTR);
Money=floatadd (Money,num);
}
document.getElementById ("Amount"). InnerHTML =money;
}
</script>
<body>
Please enter add and subtract formula: <input id= "buy" name= "buy" maxlength= "" type= "text" onkeyup= "Javascript:result ();" >
<span id= "Amount" ></span>
</body>

JS Implementation of text box support add and subtract operation method

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.