JavaScript中的出生日期验证

时间:2016-01-15 18:07:48

标签: javascript html validation date

我正在尝试验证生日日期,但我只能验证是否全部填写了。 如果我只选择日期,则没有警报(“必须选择日期/必须选择月份”)。 我无法验证日期...... 如何运行此验证?

function isbday(day,month,year) {

		var mth = month;
		var dy = day;
		var yr = year;

        if((mth < 1) || (mth > 12));
        else if((dy < 1) || (dy > 31));
        else if(((mth == 4) || (mth == 6) || (mth == 9) || (mth == 11)) && (dy > 30));
        else if((mth == 2) && (((yr % 400) == 0) || ((yr % 4) == 0)) && ((yr % 100) != 0) && (day > 29));
        else if((mth == 2) && ((yr % 100) == 0) && (dy > 29));
        else if((mth == 2) && (dy > 28)) valid = false;

    return false;
	alert("Birthdate must be filled");	

}

function validate(){
	 var day = document.getElementsByName("xday")[0].value;
	 var month = document.getElementsByName("xmonth")[0].value;
	 var year = document.getElementsByName("xyear")[0].value;
	 
  if(day,month,year == null || day,month,year == ""){
				 alert("Birthdate must be filled");	
				 
			}else if(birthdate.match(isbday(birthdate))){
				 alert("Birthdate must be filled");	
              }else
				 {
				alert("Birthdate success");
			}
		}
<tr>
		<td >Birthdate        </td>
                    <td><select name="xday"> 
                    <option value="">Date</option>
                    <script>
              var myDate = new Date();
              var year = myDate.getFullYear();
              for(var i = 1; i < 32; i++){
                  document.write('<option value="'+i+'">'+i+'</option>');
              }
              </script>
                    </select>
                    
                    <select name="xmonth">
                    <option value="">Month</option>
                   <script>
              var myDate = new Date();
              var year = myDate.getFullYear();
              for(var i = 1; i < 13; i++){
                  document.write('<option value="'+i+'">'+i+'</option>');
              }
              </script>
                    </select>
                    
                    <select name="xyear">
                    <option value="">Year</option>
                <script>
              var myDate = new Date();
              var year = myDate.getFullYear();
              for(var i = 1950; i < year; i++){
                  document.write('<option value="'+i+'">'+i+'</option>');
              }
              </script>
                </select>
</td>
	</tr>
 <tr>
    <td>
    	<input type="button" name="btnRegis" onClick="validate()" onsub value="Register"/>
       </td>
    </tr>

1 个答案:

答案 0 :(得分:0)

我认为最好的方法是根据所选月份更改日期选择,但如果您仍想以相同方式执行此操作:

稍微改变你的有效功能

每个if语句返回 false 如果无效

CREATE TABLE customer (
  customer_id INT NOT NULL,
  last_name VARCHAR(15) NULL,
  first_name VARCHAR(10) NULL,
  street_add VARCHAR(20) NULL,
  apt_num INT NULL,
  city VARCHAR(20) NULL,
  state VARCHAR(15) NULL,
  zip_code INT NULL,
  home_phone VARCHAR(10) NULL,
  mobile_phone VARCHAR(10) NULL,
  other_phone VARCHAR(10) NULL,
  customer_notes VARCHAR(45) NULL,
  PRIMARY KEY(customer_id)
);

CREATE TABLE invoice(
  invoice_id INT NOT NULL,
  customer_id INT NOT NULL,
  order_date DATE NULL,
  spec_order_note VARCHAR(45) NULL,
  PRIMARY KEY(invoice_id, customer_id),
  FOREIGN KEY (customer_id)
  REFERENCES customer (customer_id)
  ON DELETE CASCADE
  ON UPDATE CASCADE
);

CREATE TABLE line_item (
  invoice_id INT NOT NULL,
  donut_id INT NOT NULL,
  quantity INT NULL,
  CONSTRAINT donut_invoice
  FOREIGN KEY (invoice_id)
  REFERENCES invoice (invoice_id)
  ON DELETE RESTRICT
  ON UPDATE RESTRICT
);

CREATE TABLE donut (
  donut_id INT NOT NULL,
  donut_name VARCHAR(15) NULL,
  description VARCHAR(30) NULL,
  unit_price INT NULL,
  PRIMARY KEY(donut_id)
);

工作示例here