2016-07-28 4 views
0

금액을 센트로 지불하려는 지불 시스템으로 작업하고 있습니다.제출시 양식 필드의 마침표 바꾸기

혼동을 피하기 위해 고객이 10 진수로 입력 할 수있게하려고합니다. 12345 대신 123.45. 형식이 정확하도록 제출 기간을 어떻게 바꿀 수 있습니까? 나는 onclick 자바 스크립트를 구현하려고 시도했지만 구문이나 위치를 올바르게 얻지 못하는 것처럼 보입니다.

같은 형태는 그대로 여기에 있습니다 :

<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> 

내가 함께 연주 된 자바 스크립트는

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

클라이언트가 자바 스크립트를 끌 수 있으므로 서버 측에서이 작업을 수행하는 것이 좋습니다. 그것 이외에, 당신은 기간을 탈출해야합니다. 'replace (/\./, "") –

+2

당신이해야 할 일은 100을 여러 번하는 것입니다. – nerdlyist

답변

0

사용자가 센트로 필요한 금액을 입력하고 유로화로 표시하는 경우 소수를 제거하는 대신 사용자가 필요한 금액을 유로화로 입력 한 다음 센트로 변환해야하는 이유는 무엇입니까?

이상적으로 서버에서이 변환을 처리해야합니다. 입력 및 달러를 받아 들여 센트 자체로 변환 할 수 있도록 서버 코드를 수정하십시오. (0120) 하지만 고객에게 js를 사용해야한다고 알릴 필요가 있습니다. 다음

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

과 같은 것을 함께 아래의 코드

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

를 교체 차단 제출 및 onsubmit를 통해 센트에이 값을 변환; 당신의 <form ...> 태그에 onsubmit 속성 (텍스트 요소를 얻을 수있는 ID)를 추가

<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

자바 스크립트가 해제되어 있으면 잘못된 값이 서버로 전송됩니다. –

0

가 고려를 이 작업은 서버 쪽과 클라이언트 쪽 모두에서 수행됩니다. 클라이언트 측에 대한 이런 식으로 뭔가를 시도 : 당신이 당신의 양식 필드에 ID 대신 이름을 사용해야합니다

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

참고.