Reputation: 7
I'm encountering a problem in validating input fields, I have three input fields, one for first name, last name, and date of birth, when I leave the fields empty it shows warning messages as defined in the code, when I enter data into the fields the message disappears except for the date field. It disappears only when I click once again. Here is my code
<!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>
can y'all provide me a solution for this?. All answers are appreciated. Thanks in advance.
Upvotes: 0
Views: 1402
Reputation: 1122
As your code is currently structured, the focusout
and blur
events will both be triggered before the value of $('#date-of-birth')
has been set. It will not run again after the value is set unless you enter and leave the field again.
Instead of calling your validate_date_of_birth()
on focusout or blur, consider adding an onClose
function to your datepicker definition with your validate logic. This way it will not run the validation until the datepicker has closed and a value has been assigned.
i.e.,
$(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();
}
}
});
});
See this question for an example of how onClose
works, or the jQuery documentation.
Upvotes: 1