在2checkout中提交之前验证表单

时间:2015-04-09 12:52:52

标签: javascript jquery html 2checkout

我想阻止表单提交所以我尝试了下面的代码,这通常有用。但是当我使用2checkout js(https://www.2checkout.com/static/checkout/javascript/direct.js)时它无效。

<form onSubmit="validate(); return false;" method='post'>

这是js fiddle http://jsfiddle.net/0a0th3fy/1/

以下是示例代码

<form onSubmit="validate(); return false;" action='https://www.2checkout.com/checkout/purchase' method='post'>
    <input type='hidden' name='sid' value='1303908' />
    <input type='hidden' name='mode' value='2CO' />
    <input type='hidden' name='li_0_type' value='product' />
    <input type='hidden' name='li_0_name' value='invoice123' />
    <input type='hidden' name='li_0_price' value='25.99' />
    <input id="fullname" name='card_holder_name' value='' />
    <input type='hidden' name='street_address' value='123 Test Address' />
    <input type='hidden' name='street_address2' value='Suite 200' />
    <input type='hidden' name='city' value='Columbus' />
    <input type='hidden' name='state' value='OH' />
    <input type='hidden' name='zip' value='43228' />
    <input type='hidden' name='country' value='USA' />
    <input type='hidden' name='email' value='example@2co.com' />
    <input type='hidden' name='phone' value='614-921-2450' />
    <input name='submit' type='submit' value='Checkout' />
</form>

和javascript

function validate(){
    alert("validating");
    if($("#fullname").val()==""){
        alert("enter name");   
        return false;
    }
    else {
        return true;
    }    
}

2 个答案:

答案 0 :(得分:2)

为什么不使用jQuery的{​​{1}}?

试试这个:

formSubmit event

详细了解event.preventDefault()

答案 1 :(得分:0)

我尝试了你的代码,但它确实有效。 你确定包括jquery吗?

    
    function validate(){
    alert("validating");
    if($("#fullname").val()==""){
        alert("enter name");   
        return false;
    }
    else {
        return true;
    }    
}
I tried your code, and it's worked.
Are you sure you include jquery?

<!DOCTYPE html>
<html>
<body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script> 

<p>When you submit the form, a function is triggered which alerts some text.</p>

<form onSubmit="validate(); return false;" action='' method='post'>
    <input type='hidden' name='sid' value='1303908' />
    <input type='hidden' name='mode' value='2CO' />
    <input type='hidden' name='li_0_type' value='product' />
    <input type='hidden' name='li_0_name' value='invoice123' />
    <input type='hidden' name='li_0_price' value='25.99' />
    <input id="fullname" name='card_holder_name' value='' />
    <input type='hidden' name='street_address' value='123 Test Address' />
    <input type='hidden' name='street_address2' value='Suite 200' />
    <input type='hidden' name='city' value='Columbus' />
    <input type='hidden' name='state' value='OH' />
    <input type='hidden' name='zip' value='43228' />
    <input type='hidden' name='country' value='USA' />
    <input type='hidden' name='email' value='example@2co.com' />
    <input type='hidden' name='phone' value='614-921-2450' />
    <input name='submit' type='submit' value='Checkout' />
</form>


</body>
</html>