Flow
Flow

Reputation: 271

How to display a text when button click when all the * input are not fill and when fill the * go hide?

I would like some help where there a text where i would like to display if the button is press and this text appear * is mandatory if the input field are not fill up with those there are * how can i do this ?

This is my snippet where i have all the form created now i would want that this * is mandatory when the add button is press when the input is not fill up how can i do that ?

$('#submitbutton').click(function() {
  var passwordInput = $('#passwords')
  //Empty password validation
  if (passwordInput.val() == '') {
    $('.password-notmeet').show();
    $('#mandatoryField').show();
    // $('.password-empty').show();
    return;
  } else {
    $('#mandatoryField').hide();

  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form class="needs-validation" novalidate>

  <label id="mandatoryField" class="hiddenfield">* is mandatory</label>

  <div class="form-group row nameform">
    <label for="validationName" class="col-form-label nameadduser">*Name:</label>
    <div class="col-6">
      <input name="validationName" type="text" class="form-control" id="validationName" placeholder="Name" required>
      <div class="invalid-feedback">
        Enter a Name!
      </div>
    </div>
  </div>

  <div class="form-group row">
    <label for="validationEmail" class="col-form-label emailadduser">*Email:</label>
    <div class="col-6">
      <input name="validationEmail" type="email" class="form-control emails" id="validationEmail" placeholder="[email protected]" pattern="^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$"
        required>
      <div style="margin-left: 3px;" class="invalid-feedback">
        Enter a correct Email format!
      </div>
    </div>
  </div>



  <div class="form-group row">
    <label for="validationUserName" class="col-form-label usernameadduser">*Username:</label>
    <div class="col-6 ">
      <input name="validationUserName" type="text" class="form-control " id="validationUserName" placeholder="Username" pattern="^(?=.{4,}$)(?:[a-zA-Z\d]+(?:[a-zA-Z\d])*)+$" required>
      <div class="invalid-feedback">
        Username must meet the following requirements:

        <b>At least four letter</b>
      </div>

    </div>
  </div>



  <div class="form-group row">
    <label for="validationpassword" class="col-form-label passwordadduser">*Password:</label>
    <div class="col-6 d-flex">
      <input name="validationpassword" onChange="onChange()" type="password" class="form-control pass" id="passwords" placeholder="Password" pattern="^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[!@#$%^&*_=+-]).{7,}$" required>
      <i class="bi bi-eye-slash" id="togglePassword"></i>
      <!-- <div style="margin-left: 15px;" class="invalid-tooltip password-empty" >
                                    Enter a password!
                                </div> -->
      <div id="passwordvalidator" class="invalid-tooltip password-notmeet">
        Password must meet the following requirements:
        <br><br>
        <label class="color-text"> At least one capital letter</label>
        <br>
        <label class="color-text"> At least one special character</label>
        <br>
        <label class="color-text"> At least one number</label>
        <br>
        <label class="color-text"> Be at least 7 letter</label>
      </div>
    </div>
  </div>

  <div class="form-group row">
    <label for="validationconfirmpassword" class="col-form-label passwordadduser">*Re-enter<br>&#10;Password:</label>
    <div class="col-6 d-flex">
      <input name="validationconfirmpassword" onChange="onChange()" type="password" class="form-control confirmpass" id="confirm_password" placeholder="Confirm Password" required>
      <i class="bi bi-eye-slash" id="toggleconfirmPassword"></i>
      <div style="margin-left: 20px;" class="invalid-tooltip">
        Password do not match or value is null!
      </div>
    </div>
  </div>

  <div id="passwordconfirm"></div>

  <div class="form-group row">
    <label for="validationRole" class="col-form-label role">*Role:</label>
    <div class="col-3 ">
      <select class="select-user-role custom-select-sm col-11" id="select-user-role" required>
        <option class="selectrole" selected disabled value="">Select ....</option>
      </select>
      <div style="margin-left: 10px;" class="invalid-feedback">
        Enter a valid user role!
      </div>
    </div>
  </div>

  <button type="submit" class="btn-primary submitbutton">Add</button>

Upvotes: 0

Views: 38

Answers (1)

Professor Abronsius
Professor Abronsius

Reputation: 33813

With some modifications to the HTML to try to make it valid - is this what you were trying to do more or less?

When the Add button is clicked the form is initially prevented from the default submit behaviour so that ALL input elements might be checked to see if the value is empty. If it is empty a clone of the template contents is made ( with the * is mandatory text ) and appended to the parent node. As I am unfamiliar with jQuery the following is all done with vanilla Javascript but no doubt there are some slick jQuery methods that could be used to replace portions of this?!

document.querySelector('button[type="submit"]').addEventListener('click',function(e){
  e.preventDefault();
  let errors=[];
  
  let col=document.querySelectorAll('input,textarea');
      col.forEach(n=>{
        
        // remove any existing error(mandatory) messages
        let span=n.parentNode.querySelector('span.mandatory');
        if( span!==null ){
          n.parentNode.removeChild(span)
        }
        
        // if the value is empty, add the error (mandatory) message
        if( n.value=='' ){
            let tmpl=document.querySelector('template').content.cloneNode(true);
                tmpl.querySelector('i').textContent=n.name;
            n.parentNode.appendChild( tmpl );
            errors.push(n.name);
          }
      });
      
  if( errors.length==0 ){
    alert('OK - submit the form');
  }
});




const onChange=(e)=>{
  let css={
    err:'invalid',
    ok:'valid'
  };
  
  let col=document.querySelectorAll('.pwdcheck');
      col.forEach(n=>{
        n.classList.remove(css.err,css.ok);
        
        if( col[0].value!=col[1].value )n.classList.add(css.err);
        else n.classList.add(css.ok);
      });
      
};
span.mandatory{
  color:red;
  display:inline-block;
  padding:0.25rem;
  margin:0,5rem 0;
  border:1px solid red;
  background:pink
}
span.mandatory > i{
  color:white;
}
.invalid-feedback{
  opacity:0;
}
.visible{
  opacity:1;
}
label.col-form-label:before{
  content:'*';
  color:red;
}
.invalid{
  background:pink;
  color:white;
}
.valid{
  background:rgba(0,255,0,0.5);
}
<script src='//cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js'></script>
<form class='needs-validation' novalidate>

  <div class='form-group row nameform'>
    <label for='validationName' class='col-form-label nameadduser'>Name:</label>
    <div class='col-6'>
      <input 
        name='validationName' 
        type='text'
        class='form-control'
        id='validationName'
        placeholder='Name'
        required />
        
      <div class='invalid-feedback'>
        Enter a Name!
      </div>
    </div>
  </div>

  <div class='form-group row'>
    <label for='validationEmail' class='col-form-label emailadduser'>Email:</label>
    <div class='col-6'>
      <input 
        name='validationEmail' 
        type='email' 
        class='form-control emails'
        id='validationEmail' 
        placeholder='[email protected]' 
        pattern='^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$'
        required />
        
      <div style='margin-left: 3px;' class='invalid-feedback'>
        Enter a correct Email format!
      </div>
    </div>
  </div>



  <div class='form-group row'>
    <label for='validationUserName' class='col-form-label usernameadduser'>Username:</label>
    <div class='col-6 '>
      <input 
        name='validationUserName' 
        type='text'
        class='form-control'
        id='validationUserName' 
        placeholder='Username' 
        pattern='^(?=.{4,}$)(?:[a-zA-Z\d]+(?:[a-zA-Z\d])*)+$' 
        required />
        
      <div class='invalid-feedback'>
        Username must meet the following requirements:
        <b>At least four letter</b>
      </div>

    </div>
  </div>



  <div class='form-group row'>
    <label for='passwords' class='col-form-label passwordadduser'>Password:</label>
    <div class='col-6 d-flex'>
      <input 
        name='validationpassword'
        onChange='onChange(event)'
        type='password'
        class='form-control pass pwdcheck'
        id='passwords'
        placeholder='Password'
        pattern='^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[!@#$%^&*_=+-]).{7,}$'
        required />
        
      <i class='bi bi-eye-slash' id='togglePassword'></i>

      <div id='passwordvalidator' class='invalid-tooltip password-notmeet'>
        Password must meet the following requirements:
        <div class='color-text'> At least one capital letter</div>
        <div class='color-text'> At least one special character</div>
        <div class='color-text'> At least one number</div>
        <div class='color-text'> Be at least 7 letter</div>
      </div>
    </div>
  </div>

  <div class='form-group row'>
    <label for='confirm_password' class='col-form-label passwordadduser'>Re-enter<br>&#10;Password:</label>
    <div class='col-6 d-flex'>
      <input 
        name='validationconfirmpassword'
        onChange='onChange(event)'
        type='password'
        class='form-control confirmpass pwdcheck'
        id='confirm_password'
        placeholder='Confirm Password'
        required />
        
      <i class='bi bi-eye-slash' id='toggleconfirmPassword'></i>
      <div style='margin-left: 20px;' class='invalid-tooltip'>
        Password do not match or value is null!
      </div>
    </div>
  </div>

  <div id='passwordconfirm'></div>

  <div class='form-group row'>
    <label for='select-user-role' class='col-form-label role'>Role:</label>
    <div class='col-3'>
    
      <select class='select-user-role custom-select-sm col-11' id='select-user-role' required>
        <option value='' class='selectrole' selected disabled hidden>Select ....
      </select>
      
      <div style='margin-left: 10px;' class='invalid-feedback'>
        Enter a valid user role!
      </div>
    </div>
  </div>

  <button type='submit' class='btn-primary submitbutton'>Add</button>
</form>



<template>
  <span class='mandatory'>* <i></i> is mandatory</span>
</template>

Upvotes: 1

Related Questions