I took some code from online to make validation for the first name. It works however I would like to make some adjustments. I would like the error message to disappear when the field is empty. Also I would like to add margin to text as it's uneven with the next field but can't seem to make it work.
Here's the markup with the Javascript. This uses bootstrap and another styling sheet which are included below.
$(document).ready(function () {
var $regexname = /^([a-zA-Z]{2,16})$/;
$('.name').on('keypress keydown keyup', function () {
if (!$(this).val().match($regexname)) {
// there is a mismatch, hence show the error message
$('.emsg').removeClass('hidden');
} else {
// else, do not display message
$('.emsg').addClass('hidden');
}
});
});
.signup2container-stage1 {
width: 88%;
margin: auto;
}
#signupsectioncontainer-stage2 {
float: left;
width: 45%;
height: 2000px;
background-color: orange;
margin-top: 20px;
}
#signupsectioncontainer-stage3 {
width: 80%;
height: 90%;
background-color: #ffffff;
margin: auto;
}
#signup2validationinputs input {
width: 100%;
height: 45px;
margin: 10px 0px 10px 0px;
}
.signuptextinputsstyle1 {
outline-width: 0;
background-color: #e3e5e8;
border: none;
padding: 10px 10px 10px 10px;
float: left;
display: block;
}
#signup2submitbutton {
width: 100%;
margin-top: 10px;
padding: 20px 10px 20px 10px;
border: none;
outline-width: 0;
color: #ffffff;
background-color: #4caf50;
}
#signup2submitbutton #signup2submitbutton:hover {
background-color: #48a44d;
}
.emsg {
color: red;
}
.hidden {
visibility: hidden;
}
<link rel="stylesheet" type="text/css" href="https://stackpath.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>
<div class="clear">
</div>
<div class="signup2container-stage1">
<div id="signupsectioncontainer-stage2">
<h1 id="signup2title">Sign Up</h1>
<div id="signupsectioncontainer-stage3">
<div id="signup2validationinputs">
<input class="name signuptextinputsstyle1" type="text" placeholder="First Name" required/>
<p>
<span id="validname" class="emsg hidden">Please Enter a Valid Name</span>
</p>
<input class="signuptextinputsstyle1" type="text" name="" placeholder="Last Name">
<input class="signuptextinputsstyle1" type="text" name="" placeholder="Choose a Username">
<input class="signuptextinputsstyle1" type="text" name="" placeholder="Date Of Birth">
<input class="signuptextinputsstyle1" type="text" name="" placeholder="Email">
<input class="signuptextinputsstyle1" type="password" name="" placeholder="Create Password">
<input class="signuptextinputsstyle1" type="password" name="" placeholder="Confirm Password">
<button id="signup2submitbutton">SIGN UP</button>
</div>
</div>
</div>
</div>