将复选框验证添加到表单

时间:2017-07-07 09:03:45

标签: javascript html

我无法弄清楚如何在表单上添加验证,将数据发送到付款系统。

我已经在文本和数字字段的表单中添加了验证,但我根本无法弄清楚是否在复选框上进行验证。

下面你可以看到HTML和javascript代码,一些信息被遗漏了 - 我做错了什么?

由于

<script type="text/javascript" src="https://ssl.ditonlinebetalingssystem.dk/integration/ewindow/paymentwindow.js"></script>
<script type="text/javascript">

	    var iMinorUnit = 0;
			
	    function startPayment(iType)
	    {
	        var objForm = document.getElementById("ePay");
	        var objAmount = objForm.useramount;

            if (objForm.merchantnumber.value.length != 7) {
	            alert("Du mangler at indtaste dit merchant!");
	            return false;
	        }
            
            if (objForm.orderid.value.length < 1)
		    {
			    alert("Du mangler at angive dit fakturanummer eller referencen til din betaling!");
			    objForm.orderid.focus();
			    return false;
		    }

		    if (objAmount.value.length < 1 || objAmount.value == "") {
		        alert("Indtast venligst beløbet.");
		        objAmount.focus();
		        return false;
		    }
		    else if (objAmount.value.indexOf(".") > -1) {
		        alert("Beløbet må ikke indeholde tegnet '.' (punktum).\n\nDecimaler skal angives med ','. F.eks. 199,95");
		        objAmount.focus();
		        return false;
		    }


		    var amount = trimString(objAmount.value);

		    if (iMinorUnit == "-1") {
		        alert("Du har indtastet et ugyldigt beløb!\n\nBeløbet kan kun indeholde tal.");
		        objAmount.focus();
		        return false;
		    }
		    else if (iMinorUnit == "0")
		        amount = parseInt(amount) * 100;

		    var descr = "Kundens navn: " + objForm.customername.value + "<br>Evt. kommentar: " + objForm.customercomment.value;
		    
            paymentwindow = new PaymentWindow({

		        'windowstate': String(iType),

		        'paymentcollection': 1,

		        'language': 0,

		        'merchantnumber': objForm.merchantnumber.value,

		        'windowid': 1,

		        'orderid': objForm.orderid.value,

		        'amount': amount,

		        'currency': "208",

		        'description': escape(descr),

		        'ordertext': "Online betaling af faktura",

		        'accepturl': objForm.accepturl.value,

		        'cancelurl': objForm.cancelurl.value,
                
                'textonstatement': objForm.textonstatement.value
		    });

		    paymentwindow.open();
              
       
	    }
          
	    function trimString(str) {
	        iMinorUnit = 0;
	        var tmp = String(str);
	        var returnVal = "";
	        var cVal = "";

	        if (tmp != "") {
	            for (var i = 0; i < tmp.length; i++) {
	                cVal = tmp.substr(i, 1);

	                if (!isNaN(cVal))
	                    returnVal = returnVal + cVal;
	                else {
	                    if (cVal != ",") {
	                        iMinorUnit = -1;
	                        break;
	                    }
	                    else
	                        iMinorUnit = 1;
	                }
	            }
	        }

	        return returnVal;
	    }
	    
	    function init()
	    {
	        var sURL = String(window.location);
	        var arrURL = sURL.split("/");
	        var sAcceptURL = "";
	        var objForm = document.getElementById("ePay");
	        var n;
	        
	        for(n = 0; n < arrURL.length - 1; n++)
	            sAcceptURL += arrURL[n] + "/";

	        objForm.cancelurl.value = sURL;
	        objForm.accepturl.value = sAcceptURL + "/epay-gennemfoert/";
        }
	    
	    
	</script>
<form id="ePay" style="max-width: 600px;">        
    <b><u>Online betaling</u></b>
    Alle felter, undtaget kommentarfelt skal udfyldes.
    <br /><br />
    <table cellspacing="4" cellpadding="4">
	    <tr>
	        <td style="max-width: 700px; border: 0px;"><input type="text" placeholder="Fakturanummer" name="orderid" maxlength="20" style="width: 200px;" /></td>
        </tr>
        <tr>
	        <td style="border: 0px;"><input type="text" placeholder="Beløb i DKK"name="useramount" style="width: 200px;" /></td>
        </tr>
        <tr>
	        <td style="border: 0px;"><input type="text" placeholder="Navn" name="customername" style="width: 200px;" maxlength="50" /></td>
        </tr>
        <tr>
	        <td style="border: 0px;"><textarea name="customercomment"  placeholder="Evt. kommentar" style="width: 300px; height: 100px;"></textarea></td>
        </tr>
        <tr>
               <td  style="border: 0px;">
	       <input type="checkbox" name="checkbox" value="check" id="terms" required />Jeg har læst og accepteret alle <a href="http://#"><u>handelsbetingelser</u></a></td></tr>
              <tr> <td colspan="3" style="text-align: left; padding-top: 10px; border: 0px;"><input type="button" value="Betal" onclick="startPayment(3)" style="width: 180px;" /></td>
        </tr>
        <tr>
            <td colspan="3" style="padding-top: 30px; border: 0px;">
                <table cellspacing="0" cellpadding="0">
                    <tr>
                        <td style="vertical-align: top; padding-right: 20px; border: 0px;"><a href="http://www.epay.dk" target="_blank"><img src="/epay/epay-payment-solutions.gif" width="75" height="44" style="border: 0px;" alt="ePay / Payment Solutions" /></a></td>
                        <td style="vertical-align: top; font-size: 11px; padding-top: 2px; border: 0px;">
                            <b>SIKKER BETALING</b>
                            <br />
                            <span class="notranslate">ePay / Payment Solutions</span> er PCI certificeret af VISA / Mastercard,<br />og al kommunikation foregår krypteret via en sikker forbindelse.  
                        </td>  
                    </tr>
                </table>
            </td>
        </tr>
    </table>
    <input type="hidden" name="merchantnumber" value="8025944"/>
    <input type="hidden" name="accepturl" value="" />
    <input type="hidden" name="cancelurl" value="" />
    <input type="hidden" name="textonstatement" value=""/>
</form>

2 个答案:

答案 0 :(得分:0)

这不应该很简单吗? (Apart from the security gaps)

<form>
    Needed Checkbox: <input type="checkbox" required>
    <input type="submit" />
</form>
<!-- brings up its own error when it fails -->

Alternativ:https://jsfiddle.net/w7baj016/

根据你的评论,这是我制作的jquery样本。愿这有助于你的行为

您应该关注文档是否已满载:$(document).ready()

//care if fully loaded
$("#myForm").on('submit',function(e){

    e.preventDefault();
    //i guess in js you can simply call event.preventDefault();

    if($("#some").prop("checked") == false){
        alert("You need to Check this first");
    }else{
        this.submit();
    }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="myForm">
    <input id="some" type="checkbox">
    <input type="submit" />
</form>

简单JS代码

How to care about the document load state with js

//you should care that the document is fully loaded (ready)
document.querySelector("#myForm").addEventListener("submit", function(e){
    
    e.preventDefault();    //stop form from submitting
    var isValid = document.getElementById("some").checked;

    if(isValid){
        this.submit();
    }else{
        alert("please check the checkbox befor");
    }
});
<form id="myForm">
<input id="some" type="checkbox">
<input type="submit" />
</form>

我希望这有助于你解决这个问题:)

答案 1 :(得分:0)

如果您不想从js lib / framework获得支持,只需将以下代码添加到开始付款即可。但我强烈建议使用jquery或angular。

for d in data:
    print(convert(d))

#23:59:59:58
#23:59:59:80
#23:59:59:99
#00:00:00:20
#00:00:00:40
#00:00:00:60