simpleCart(js)将表单输入验证添加到电子邮件签出提交

时间:2012-03-28 20:01:34

标签: javascript

我在表单中添加了一个验证,它生成一个窗口,说明该字段为空,问题是它仍在提交表单,即使它在无效字段上返回false。

<script type="text/javascript">
function validateForm(){
var x=document.forms["form"]["name"].value;
if (x==null || x=="")
  {
  alert("First name must be filled out");
  return false;
  }
var atpos=x.indexOf("@");
var dotpos=x.lastIndexOf(".");
if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length)
  {
  alert("Not a valid e-mail address");
  return false;
  }
}
</script>


<form action="sendjs.php"  name="form" >
                <em><strong>Fill in the fields below BEFORE pressing checkout.</strong></em><br />
                  <input placeholder="Name" type="text" name="name"  value="" id="name" title="Please enter a name!" />
                  <input placeholder="Phone Number" type="text" name="phone" value="" id="phone" />
                  <input placeholder="Email" type="text" name="email" value="" id="email"title="Please enter an email!" />
         <input type="submit"  onclick="return validateForm();" class="simpleCart_checkout" value="Checkout"/>
                </form>

这是simpleCart.js

me.emailCheckout = function kiwi() {    

itemsString = "";
for( var current in me.items ){ 
    var item = me.items[current];
    itemsString += item.name + " " + item.quantity + " " + item.price + "\n";
}   

name = document.form.name.value;
phone = document.form.phone.value;
email = document.form.email.value;


var form = document.createElement("form");
form.style.display = "none";
form.method = "POST";
form.action = "sendjs.php";
form.acceptCharset = "utf-8";
form.appendChild(me.createHiddenElement("jcitems", itemsString));
form.appendChild(me.createHiddenElement("jctotal", me.total));
form.appendChild(me.createHiddenElement("name", name));
form.appendChild(me.createHiddenElement("phone", phone));
form.appendChild(me.createHiddenElement("email", email));
document.body.appendChild(form);    
form.submit();
document.form.submit();
document.body.removeChild(form);
}

1 个答案:

答案 0 :(得分:1)

你可以这样做:

simpleCart.bind('beforeAdd',function(item){
    if(!item.name){
        // edit the dom if needed
        return false;
    }
});