jQuery验证日期字段

时间:2018-08-10 15:12:36

标签: javascript jquery html css

我在验证输入字段时遇到问题,我有三个输入字段,一个用于名字,姓氏和生日,当我将这些字段留空时,它会显示代码中定义的警告消息,当我将数据输入到消息消失的字段中,但日期字段除外。仅当我再次单击时,它才会消失。这是我的代码

<!DOCTYPE html>
<html>
<head id="html">
	<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
	<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
	<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"></script>
	<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>
	<link rel="stylesheet" href="https://ajax.aspnetcdn.com/ajax/jquery.ui/1.10.4/themes/redmond/jquery-ui.css">
	<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"> </script>
	<script>
		$(function() {
			$("#date-of-birth").datepicker(
			{
				dateFormat:'dd-mm-yy',
				changeMonth: true,
				changeYear: true,
			});	
		});
	</script>
	<style >
		#first-name{
			width: 300px;
			margin-top: 10px;
			margin-left: 510px;
			padding-left: 10px;
			border: 1px solid grey;
			border-radius: 5px;
		}
		#last-name{
			width: 300px;
			margin-top: 5px;
			margin-left: 510px;
			padding-left: 10px;
			border: 1px solid grey;
			border-radius: 5px;
		}
		#date-of-birth{
			width: 300px;
			margin-top: 5px;
			margin-left: 510px;
			padding-left: 10px;
			border: 1px solid grey;
			border-radius: 5px;	
		}
		#first-name-warning-message{	
			padding-left: 640px;
			font-size: 18px;
			font-weight: bold;
		}
		#last-name-warning-message{
			padding-left: 640px;
			font-size: 18px;
			font-weight: bold;
		}
		#dob-warning-message{
			padding-left: 640px;
			font-size: 18px;
			font-weight: bold;
		}
	</style>
</head>
<body id="body">
	<form id="sign-up" action="#" method="post">
		<div id="row-one">
			<input type="text" id="first-name" name="first_name" placeholder="First Name" maxlength="15">
		</div>
		<div id="span-container">
			<span id="first-name-warning-message" class="text-danger"> </span>
		</div>
		<div id="row-two">
			<input type="text" id="last-name" name="last_name" placeholder="Last Name" maxlength="15">
		</div>
		<div id="span-container">
			<span id="last-name-warning-message" class="text-danger"> </span>
		</div>
		<div id="row-three">
			<input class="date-picker" id="date-of-birth" name="user_dob" placeholder="Date Of Birth" readonly="true">
		</div>
		<div id="span-container">
			<span id="dob-warning-message" class="text-danger"> </span>
				</div>
	</form>
	<script>
		$(document).ready(function(){
			$('#first-name-warning-message').hide();
			$('#last-name-warning-message').hide();
			$('#dob-warning-message').hide();

			var first_name_error=true;
			var last_name_error=true;
			var dob_error=true;

			$('#first-name').focusout(function(){
				validate_first_name();
			});

			$('#last-name').focusout(function(){
				validate_last_name();
			});

			$('#date-of-birth').focusout(function(){
				validate_date_of_birth();
			});

			function validate_first_name(){
				var first_name=$('#first-name').val();
				var first_name_regex=/^[a-zA-Z ]{3,15}$/;
				if(first_name.length==''){
					$('#first-name-warning-message').show();
					$('#first-name-warning-message').html("Please Enter Your First Name !");
					first_name_error=false;
					return false;
				}
				else{
					$('#first-name-warning-message').hide();
				}

				if(first_name.length<3){
					$('#first-name-warning-message').show();
					$('#first-name-warning-message').html("First Name Not Valid !");
					first_name_error=false;
					return false;
				}
				else{
					$('#first-name-warning-message').hide();
				}

				if(!first_name_regex.test(first_name)){
					$('#first-name-warning-message').show();
					$('#first-name-warning-message').html("First Name Must Not Contain Any Digits Or Symbols !");
					first_name_error=false;
					return false;
				}
				else{
					$('#first-name-warning-message').hide();
				}
			}

			function validate_last_name(){
				var last_name=$('#last-name').val();
				var last_name_regex=/^[a-zA-Z ]{3,15}$/;

				if(last_name.length==''){
					$('#last-name-warning-message').show();
					$('#last-name-warning-message').html("Please Enter Your Last Name !");
					last_name_error=false;
					return false;
				}
				else{
					$('#last-name-warning-message').hide();
				}

				if(last_name.length<3){
					$('#last-name-warning-message').show();
					$('#last-name-warning-message').html("Last Name Not Valid !");
					last_name_error=false;
					return false;
				}
				else{
					$('#last-name-warning-message').hide();
				}

				if(!last_name_regex.test(last_name)){
					$('#last-name-warning-message').show();
					$('#last-name-warning-message').html("Last Name Must Not Contain Any Digits Or Symbols !");
					last_name_error=false;
					return false;
				}
				else{
					$('#last-name-warning-message').hide();
				}
			}

			function validate_date_of_birth(){
				var date_of_birth=$('#date-of-birth').val();
				if(date_of_birth.length==''){
					$('#dob-warning-message').show();
					$('#dob-warning-message').html("Please Enter Your Date Of Birth !");
					dob_error=false;
					return false;
				}
				else{
					$('#dob-warning-message').hide();
				}
			}
		});
	</script>
</body>
</html>

可以为我提供解决方案吗?所有的答案表示赞赏。预先感谢。

1 个答案:

答案 0 :(得分:1)

由于您的代码当前已结构化,因此focusoutblur事件都将在设置$('#date-of-birth')的值之前触发。除非您再次输入并离开该字段,否则在设置该值后它将不会再次运行。

考虑用验证逻辑将validate_date_of_birth()函数添加到datepicker定义中,而不是聚焦或模糊调用onClose。这样,只有关闭日期选择器并分配了值之后,它才会运行验证。

$(function() {
    $("#date-of-birth").datepicker({
        dateFormat:'dd-mm-yy',
        changeMonth: true,
        changeYear: true,
        onClose: function(selectedDate){
            var date_of_birth=selectedDate;
            if(date_of_birth.length==''){
                $('#dob-warning-message').show();
                $('#dob-warning-message').html("Please Enter Your Date Of Birth !");
                dob_error=false;
                return false;
            }
            else{
                $('#dob-warning-message').hide();
            }
        }
    }); 
});

请参见此question,以获取onClosejQuery documentation的工作方式示例。