0

我正在使用 EVOPDF 在 mvc 应用程序中使用 cshtml 代码生成 pdf。我的 cshtml 文件中有 4 个用于金额的文本框,还有 1 个用于 TotalAmount 的文本框。总金额的值将是所有 4 个文本框值的总和。我已经使用下面的 c# 代码实现了这一点

pdfDocument.Form.Fields["qyt1"].Action = new PdfActionJavaScript(JaveScriptfunctionForCurrentYear()) 

private string JaveScriptfunctionForCurrentYear()  //C# method, returning js code as string to calcuate Total amount
{
return " this.getField('totqyt').value = (parseInt((this.getField('qyt1').value=='')?0:this.getField('qyt1').value) + parseInt((this.getField('qyt2').value=='')?0:this.getField('qyt2').value) + parseInt((this.getField('qyt3').value=='')?0:this.getField('qyt3').value)+ parseInt((this.getField('qyt4').value=='')?0:this.getField('qyt4').value));";
}

Total 的计算仅适用于数值。当用户用逗号输入数值时,上面的代码会中断。因此,为了处理这种情况,我修改了计算总金额的方法,如下所示。但是下面的代码不适用于没有逗号的数字。

 private string JaveScriptfunctionForCurrentYear()  //C# method, returning js code as string
    {
        return " this.getField('totqyt').value =  " +
        " + parseInt((this.getField('qyt2').value =='')?0:this.getField('qyt2').value.replace(',','')) " +
        " + parseInt((this.getField('qyt2').value =='')?0:this.getField('qyt2').value.replace(',','')) " +
        " + parseInt((this.getField('qyt3').value =='')?0:this.getField('qyt3').value.replace(',',''))" +
        " + parseInt((this.getField('qyt4').value =='')?0:this.getField('qyt4').value.replace(',',''))";
    }

除此之外,用户还可以输入十进制值,也应将其汇总为总金额。

因此,如果用户输入带逗号的数值和十进制值,请指导我如何计算总金额。我想防止用户输入任何其他可能破坏计算的字符。

4

0 回答 0