2

I am trying to style my first website. I am using Bootstrap 4 and the jquery validation plugin and I am doing a form right now. I have two problems: First, I only want to change the color of the error message below my input, but my styling changes the input text size/color and the error message text size/color, how can I only change error text below an input?

Second question is, where in my code do I change the Bootstrap form control state, depending on valid or not?

This is my code so far:

$('document').ready(function(){

    $('#registration_form').validate({
        rules: {
            username: {
                required: true,
                maxlength: 10,
                minlength: 3
            },
            email: {
                required: true,
                email: true
            },
            email_con: {
                required: true,
                equalTo: "#email"
            },
            password: {
                required: true,
                minlength: 7
            },
            password_con: {
                required: true,
                equalTo: "#password"
            },
            formCheck: {
                required: true
            },
        }
    });
});
        .error{
          color: red;
          font-Size: 13px;
        }
                                <div class="form-row form-group">
                                    <div class="col-sm-4 col-xl-3 text-center d-xl-flex justify-content-xl-center align-items-xl-center label-column"><label class="col-form-label">Email* :</label></div>
                                    <div class="col-sm-6 input-column"><input class="form-control" type="email" name="email" id="email" placeholder="[email protected]"></div>
                                </div>

3
  • 1
    where do u have the element with class "error" @BMW3000 Commented Feb 20, 2020 at 8:18
  • <script src="cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.19.1/…> I am using this source for my error validation Commented Feb 20, 2020 at 8:22
  • @BMW3000, Can you try #name-error instead of .error in css .. Commented Feb 20, 2020 at 8:28

3 Answers 3

1

If you want to change color of error text message only then write like label.error{color: red;} and follow below html structure.

$(document).ready(function(){
  $('#registration_form').validate({
    rules: {
      email: {
        required: true,
        email: true
      }
    }
  });
});
label.error{
  color: red;
  font-size: 13px;
}
<link rel="stylesheet" type="text/css" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">

<script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.19.1/jquery.validate.min.js"></script>

<div class="container my-2">
  <div class="row">
    <form class="col-sm-12" action="#" id="registration_form">
      <div class="form-row form-group">
        <div class="col-sm-4 text-sm-right">
          <label class="col-form-label" for="email">Email* :</label>
        </div>
        <div class="col-sm-6">
          <input class="form-control" type="email" name="email" id="email" placeholder="[email protected]">
        </div>
      </div>
      <div class="form-row form-group">
        <div class="col-sm-6 offset-sm-4">
          <button type="submit" class="btn btn-success">Submit</button>
        </div>
      </div>
    </form>
  </div>
</div>

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

Comments

0

There are classes to deal with validaiton when it comes to bootstrap usage. Try like this

<div class="container">
<p class="h1">Register</p>

<form class="needs-validation" novalidate>
<div class="form-group">
  <label for="email">E-mail</label>
  <input type="email" class="form-control" id="email"
         placeholder="E-mail" required>
  <div class="valid-feedback">
    Looks good!
  </div>
  <div class="invalid-feedback">
    The e-mail is required!
  </div>
</div>

<div class="form-group">
  <label for="password">Password</label>
  <input type="password" class="form-control" id="password"
         placeholder="Password" required minlength="6">
  <div class="valid-feedback">
    Great!
  </div>
  <div class="invalid-feedback">
    The password must contain at least 6 characters!
  </div>
</div>

<div class="form-group mt-3">
  <button class="btn btn-primary" type="submit">Register!</button>
</div>
</form>
</div>

<script>
(function() {
window.addEventListener('load', function() {
  var forms = document.getElementsByClassName('needs-validation');

  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>

Comments

0

You can run a snippet in full page and check this answer

<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container">
  <p class="h1">Register</p>

  <form class="needs-validation" novalidate>
    <div class="form-group">
      <label for="email">E-mail</label>
      <input type="email" class="form-control" id="email"
             placeholder="E-mail" required>
      <div class="valid-feedback">
        Looks good!
      </div>
      <div class="invalid-feedback">
        The e-mail is required!
      </div>
    </div>

    <div class="form-group">
      <label for="password">Password</label>
      <input type="password" class="form-control" id="password"
             placeholder="Password" required minlength="6">
      <div class="valid-feedback">
        Great!
      </div>
      <div class="invalid-feedback">
        The password must contain at least 6 characters!
      </div>
    </div>

    <div class="form-group mt-3">
      <button class="btn btn-primary" type="submit">Register!</button>
    </div>
  </form>
</div>

<script>
  (function() {
    window.addEventListener('load', function() {
      var forms = document.getElementsByClassName('needs-validation');

      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>

1 Comment

This is BS4 validation but in question there is define jquery-validate so 1st follow the question what is require.

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.