2016-07-28 195 views
0

我正在使用希望以美分輸出數量的付款系統。提交表單字段時提交

爲了避免混淆,我希望我的客戶能夠輸入十進制的數字123.45而不是12345。我如何更換提交期限以便格式正確?我試圖實現onclick JavaScript,但似乎沒有得到th語法或定位權。

的形式,因爲是在這裏:

<form action="https:/….Default.aspx" method="post"> 

<table> <tbody> 
<tr> 
<td>Client Reference:</td> 
<td><input name="orderid" type="text" value=" " /></td> 
<td>e.g. Smith 2017</td> 
</tr> 
<tr> 
<td>Amount in euros:</td> \<td><input name="amount" type="text" value=" " /></td> 
<td>please put the amount in cents instead of using full stop (.) e.g. 12356 instead of 123.56</td> 
</tr> 
<tr> 
<td><input type="submit" value="Pay" /></td> 
<td></td> 
</tr> 
</tbody> 
</table> 

<input name="currency" type="hidden" value="EUR" /> 
<input name="windowstate" type="hidden" value="3" /> 
<input name="merchantnumber" type="hidden" value=「/>  
</form> 

我一直在玩的JavaScript是

onsubmit = 「removeperiod()」 

<script type = "text/javascript"> 

function removeperiod() { 

var str = 「amountenter」 
str = str.replace(/./,」」); 
amountenter = str; // write the de-accented string back into the form field. 
} 
</script> 

任何幫助非常讚賞。

+1

爲什麼不在後端將他們輸入的數量除以0.01? –

+0

強烈建議在服務器端執行此操作,因爲您的客戶端可能會關閉javascript。除此之外,你需要避開這段時間。 '替換(/\./,「」)' –

+2

你所需要做的只是100的倍數。 – nerdlyist

回答

0

而不是期望您的用戶把所需的金額以美分,然後刪除小數,如果他們以歐元做它,爲什麼不期望您的用戶把所需的金額以歐元,然後將其轉換爲美分?

理想情況下,您應該在服務器上處理此轉換;修復服務器代碼(或告訴誰管理它來修復它),以便它可以接受輸入和美元,然後將其轉換爲美分本身

如果這是不可能的,那麼你可以使用JS做到這一點。但是你必須告訴你的客戶他們需要啓用js。東西替換下面的代碼

<td>please put the amount in cents instead of using full stop (.) e.g. 12356 instead of 123.56</td> 

<noscript>Please enable JS, or put the amount in cents instead of using full stop (.) e.g. 12356 instead of 123.56</noscript> 

然後攔截提交併通過onsubmit轉換該值來美分;在onsubmit屬性(和ID來獲取文本元素)添加到您的<form ...>標籤有:

<form action="https:/….Default.aspx" method="post" onsubmit="prepareToSubmit" id="form"> 

,然後實現這樣的prepareToSubmit功能轉換:

<script type = "text/javascript"> 

function prepareToSubmit() { 
    var amountElement = document.getElementById("form").elements["amount"]; 

    //Converts value from euros to cents 
    amountElement.value = Math.round(amountElement.value * 100); 

    return true; 
} 

+0

如果javascript被關閉,不正確的值將被髮送到服務器。 –

0

考慮這樣做:服務器端和客戶端。 對於客戶端嘗試這樣的事:

function removeperiod() { 
    var str = document.getElementById("amount").value; 
    str = str.replace(".",""); 
    document.getElementById("amount").value = str; 
    return true; 
} 

請注意,您必須在您的表單字段使用ID,而不是