选择字段的JavaScript表单验证,检查值不为0

时间:2016-02-19 09:27:50

标签: javascript

我有一个表单是要向数据库添加一个模块,我有一个值0,1,2,3 ... 0 =形式的选择字段无效,我知道我可以禁用第一个字段但我不知道它是否会干扰验证过程。

注意:休息工作正常,当它验证它到达模块类型(选择)时卡住了

<?php
include("../authorise.php");
authorise_user("3");
?>
<script src="https://code.jquery.com/jquery-2.2.0.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<body>
<div>
   <form id="myForm" method="post">
      <label for="module_name">Module Name:</label>
      <input name="module_name" id="module_name" type="text" /><br />
      <label for="module_code">Module Code:</label>
      <input name="module_code" id="module_code" type="text" /><br />
      <label for="module_leader">Module Leader:</label>
      <input name="module_leader" id="module_leader" type="text" /><br />
      <label for="module_type">Module Type:</label>
      <select name="module_type" id="module_type">
         <option value="0" selected="selected">Please select a module type.</option>
         <option value="1">Lecture</option>
         <option value="2">Seminar</option>
         <option value="3">Lab Practical</option>
      </select> 
      <label for="module_location">Module Location:</label>
      <input name="module_location" id="module_location" type="text" /><br />
      <label for="module_date">Module Date:</label>
      <input name="mnodule_date" id="mnodule_date" type="text" /><br />
      <input type="button" id="submitFormData" onclick="SubmitFormData();" value="Submit" />
   </form>
</div>
<div id="error"></div>
</body>
<script>
function SubmitFormData() {
   var name = $("#module_name").val();
   var code = $("#module_code").val();
   var leader = $("#module_leader").val();
   var type = $("#module_type").val();
   var location = $("#module_location").val();
   var date = $("#module_date").val();
   var validModCode = /[a-zA-Z]{3}\d{6}/
      if((name.length < 6)) {   5
         var message = "Module Name must be atleast 6 characters.";
         $('#error').html(message);
         $("#module_name").focus();
         return false; 
      } else if(code == "") {    
         var message = "Enter a Module Code.";
         $('#error').html(message);
         $("#module_code").focus();
         return false; 
      } else if(!validModCode.test(code)){
         var message = "Invalid Module Code (Format: 3 a-z characters, followed by 6 numeric digits ... e.g. MOD002769)."; 
         $('#error').html(message);
         $("#module_code").focus();
         return false; 
      } else if (leader.length < 6) {
         var message = "Module leader must be atleast 6 characters."; 
         $('#error').html(message);
         $("#module_leader").focus();
         return false;  
      } else if(type.value == 0) {
         var message = "Please choose a Module Type.";
         $('#error').html(message);
         $("#module_type").focus();
         return false;
      } else if (location.length < 6) {        
         var message = "Module location must be atleast 8 characters."; 
         $('#error').html(message);
         $("#module_location").focus();
      } else if (date.length < 6) {        
         var message = "Module date must include a day and a time."; 
         $('#error').html(message);
         $("#module_date").focus();
      } else { 
         $('#error').html("Adding Module");
         $.post("addModuleSubmit.php", { 
            name:name,
            code:code,
            leader:leader,
            type:type,
            location:location,
            date:date
      }, function(data) {
         $('#error').html(data);
         $('#myForm').trigger('reset');
      });
   }
}
</script>

1 个答案:

答案 0 :(得分:1)

这样给出

var type =  $("#module_type option:selected").val();

然后给出检查条件,如

if(type == 0){ //code }