0

Hello guys I want to restrict user from entering date manually in an input field in HTML. I want user to select date from the date-picker provided to avoid input date entry

Here is the my code in which I've used date-picker.

<!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>

Please help me I'm stuck with this date validation issue.

2
  • Add readonly to your input <input readonly /> this will avoid entering date manually by user. Commented Jul 31, 2018 at 11:50
  • thank you so much Girish Commented Jul 31, 2018 at 11:53

2 Answers 2

2

You can simply add the readonly attribute to your input :

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

Demo:

$(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>

Sign up to request clarification or add additional context in comments.

2 Comments

@JoelMathews, if his answer helped you, mark it as the right answer.
Ok Adam thank you for letting me know as I'm new to this place
0

All what you need is to add readonly='true' attribute to your input field

<!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" readonly='true'>
		</div>
	</form>
</body>
</html>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.