字段不能为NULL

时间:2014-04-09 04:54:03

标签: javascript jquery html

我如何编写一个代码,严格来说必须有一个值,而不是"立即付款"按钮不会继续。

例如对于字段

  • 卡片类型一个值必须是选定的一个
  • 并且输入卡号必须具有值
  • 和Enter Pin也必须具有值

否则"立即付款"按钮会显示"请确保所有细节都已填写。

HTML

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Terms & Conditions</title>
<link rel="stylesheet" type="text/css" href="css/format.css">
<link rel="stylesheet" href="/resources/demos/style.css">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="css/jquery.mobile-1.4.2.min.css">
<script src="js/jquery-1.10.2.min.js"></script>
 <script src ="js/jquery-1.11.0.min.js"></script>
<script src="js/jquery.cycle.all.js"></script>
<style type="text/css">
</style>
</head>
<body background="images/wallpaper.jpg">

    <h22><h1>  
<h>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Purchase&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</h>  
</h1></h22>
        <p>&nbsp;</p>
        <p>&nbsp;</p>
        <hr></hr>
        <p>&nbsp;</p>
        <h22><h1><p1><fieldset>
            <form action = "">
  <p>
    <span>
      PAYMENT GATEWAY
    </span>
    <br /><br />
    <form>
Card Type:
        <select>
<option value="" style="display:none">Select</option>
<option value="one">Master Card</option>
<option value="two">VISA Card</option>
</select>
        <p>&nbsp;</p>
Enter Card No:<input type="text" name="cardno" placeholder="0000-0000-0000"><br>
        <p>&nbsp;</p>
Enter Pin No:<input type="text" name="pinno" placeholder="000">
        <p><sil>(Your 3-Digit pin can be found at the back of your card)</sil></p>
        <p>&nbsp;</p>
<input type="submit" value="PAY NOW" onclick="alert('THANK YOU,Your Order Will Be   
Processed Shortly.Stay TUNE..');">
</form>

    <br /> <br /></p>
</form>
            </fieldset></p1></h1></h22>
        <p>&nbsp;</p>
        <p>&nbsp;</p>
        <p>&nbsp;</p>
        <p>&nbsp;</p>
</body>
</html>

2 个答案:

答案 0 :(得分:0)

您可以检查函数中所有字段的值,这将调用您表单的提交按钮。以下是相同的功能:

function validate()
{
    var cardType = $("#cardType");
    var cardNo = $("#cardno");
    var pinNo = $("#pinno");

    if(cardType[0].selectedIndex == 0 ){
        alert('Please select card type');
        return false;
    }
    if(cardNo.val() == '')
    {
        alert('Please enter card number');
        return false;
    }
    if(pinNo.val() == '')
    {
        alert('Please enter pin number');
        return false;
    }
    alert('THANK YOU,Your Order Will Be  Processed Shortly.Stay TUNE..');
    return true;
}  

我已将id提供给所有字段。根据您的要求更换选择器 这可以帮助您验证表单字段。

答案 1 :(得分:0)

您可以在此过程中使用jquery验证插件。

这里是链接,只需转到该链接并查看文档。

http://jqueryvalidation.org

您的代码可能就像这样

<script>
        $(document).ready(function () {
            $("#payment").validate({
                rules: 
                {
                    name:{
                    required:true,
                    },
                    cardno:{
                    required:true,
                    minlength:16,
                    },
                    year: {
                    required: true,
                    },
                    month: {
                    required: true,
                    },
                    cvv:{
                    required:true,
                    minlength:3,
                    },
                },
                messages:
                {   
                    name:
                    { required:"Card Holder's Name is Required"
                    },
                    cardno:
                    { required:"Card Number is Required",
                      minlength:"Enter Valid Card Number"

                    },
                    year:
                    { required:"Year is Required"
                    },
                    month:
                    { required:"Month is Required"
                    },
                    cvv:
                    { required:"CVV Number is Required",
                      minlength:"Enter Valid CVV Number"
                    },
                }
            });
                  </script>