Reputation: 21
First time using bootstrap and first time asking a question on stackoverflow! I am creating this mockup with forms for email, subject and message. I am also using bootstrap validator to get error messages to pop-up (like ""). but they appear all the way to the left of the window. Anyone know of a way to get the below the input box? Or even above the input box? Like if where says "Email" I would like the error message that says "Please enter a valid email" to pop up right next to it. I've included the html and css. Hopefully I did it right. Thanks!
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="form-issue.css">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
<script src="https://cdn.rawgit.com/PascaleBeier/bootstrap-validate/v2.2.0/dist/bootstrap-validate.js"></script>
</head>
<body>
<div class="contact">
<h2>CONTACT</h2>
<form action="" method="POST">
<div class="form-group">
<label class="label" for="email">Email</label>
<input id="email" class="form-control" type="text" name="email">
</div>
<div class="form-group">
<label class="label" for="subject"> Subject </label>
<input id="subject" class="form-control" type="text" name="subject">
</div>
<div class="form-group">
<label class="label" for="message">Message</label>
<textarea id="message" class="form-control" name="message" rows="10"></textarea>
<input class="button" type="submit" value="Submit">
</div>
</form>
</div>
<script>
bootstrapValidate('#email', 'email:Please enter a valid email');
bootstrapValidate('#subject', 'required:Subject is required');
bootstrapValidate('#message', 'required:Message is required');
</script>
</body>
</html>
.contact {
color: white;
background-color: #c0c0c0;
padding-top: 2%;
padding-bottom: 4%;
width: 100%;
}
.contact h2 {
text-align: center;
}
.form-group {
width: 100%;
display: flex;
flex-direction: column;
align-items: center;
margin: 25px;
}
#email,
#subject,
#message {
width: 30%;
}
.label {
width: 30%;
text-align: left;
}
.button {
background-color: #000000;
color: white;
text-align: center;
width: fit-content;
min-height: inherit;
border-color: #000000;
border-width: 10px;
margin-top: 20px;
margin-bottom: 5px;
}
Upvotes: 2
Views: 429
Reputation: 188
Use Bootstrap class "text-center" in class attribute of an element.
Upvotes: 0
Reputation: 25
Please see below example for validation! And add one line style: .invalid-feedback{ width:30%; }
Please see below example for validation! And add one line style: .invalid-feedback{ width:30%; }
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
<script src="https://cdn.rawgit.com/PascaleBeier/bootstrap-validate/v2.2.0/dist/bootstrap-validate.js"></script>
<script type="text/javascript">
(function() {
'use strict';
window.addEventListener('load', function() {
// Fetch all the forms we want to apply custom Bootstrap validation styles to
var forms = document.getElementsByClassName('needs-validation');
// Loop over them and prevent submission
var validation = Array.prototype.filter.call(forms, function(form) {
form.addEventListener('submit', function(event) {
if (form.checkValidity() === false) {
event.preventDefault();
event.stopPropagation();
}
form.classList.add('was-validated');
}, false);
});
}, false);
})();
</script>
</head>
<body>
<div class="contact">
<h2>CONTACT</h2>
<form action="" method="POST" class="needs-validation" novalidate>
<div class="form-group">
<label class="label" for="email">Email</label>
<input id="email" class="form-control" type="text" name="email" required="">
<div class="invalid-feedback"> Please enter valid email.</div>
</div>
<div class="form-group">
<input class="button" type="submit" value="Submit">
</div>
</form>
</div>
</body>
</html>
Upvotes: 2