限制HTML中的用户输入

时间:2018-07-31 11:43:45

标签: jquery html css

您好,我想限制用户在HTML的输入字段中手动输入日期。我希望用户从提供的日期选择器中选择日期,以避免输入日期

这是我使用日期选择器的代码。

<!DOCTYPE html>
<html class="html">
<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width,initial-scale=1">
	<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.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() {
			$("#datepicker").datepicker(
			{
				dateFormat:'dd-mm-yy',
				changeMonth: true,
				changeYear: true,
			});	
		});
	</script>
	<style>
		.date-picker{
			width: 250px;
			height: 30px;
			font-size: 18px;
			padding-left: 7px;
			margin-top: 10px;
			margin-left: 145px;
			border: 1px solid grey;
			border-radius: 5px;
		}
	</style>
</head>
<body class="body">
		<div class="date-picker-container">
			<input class="date-picker" id="datepicker" placeholder="Date Of Birth">
		</div>
	</form>
</body>
</html>

请帮我解决这个日期验证问题。

2 个答案:

答案 0 :(得分:2)

您只需将readonly属性添加到您的输入中即可:

<input class="date-picker" id="datepicker" placeholder="Date Of Birth" readonly>

演示:

$(function() {
  $("#datepicker").datepicker({
    dateFormat: 'dd-mm-yy',
    changeMonth: true,
    changeYear: true,
  });
});
.date-picker {
  width: 250px;
  height: 30px;
  font-size: 18px;
  padding-left: 7px;
  margin-top: 10px;
  margin-left: 145px;
  border: 1px solid grey;
  border-radius: 5px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.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>

<div class="date-picker-container">
  <input class="date-picker" id="datepicker" placeholder="Date Of Birth" readonly>
</div>

答案 1 :(得分:0)

您所需要做的就是在输入字段中添加readonly ='true'属性

None