Form checking does not work as expected

Hi Paul
thank you for help and patience. unfortunately I found another problem. I thought I can build the form checking as I learned but the following example doesn’t work as I expect, doing things like empty check depending on first field that is nonsense

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Bootstrap Example</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
  <script>
$(document).ready(function(){
   
   $(".form-control").on( "blur input", function( ){
   console.log('changed');
   var vr = $(this).attr('name');
   var inp = $('.form-control').val().trim();
    if ( inp === "" ){
      
	  
	  alert(vr + ' can not be left blank');
	  $(this).next().html(vr + " is empy ");
      event.preventDefault();
     } 
	else {
	  
	  $(this).next().html(vr + " is ok ");
   }
  });
  
  $(".btn1").click(function(){
  $( ".form-control" ).each(function() {
  var vr = $(this).attr('name');
    if ($('input').val().trim() === ""){
      
	  alert(vr + ' can not be left blank');
	  $(this).next().html(vr + " is empy ");
      event.preventDefault();
   } else {
	  
	  $(this).next().html(vr + " is ok ");
   }
  });
  });
});
</script>
</head>
<body>

<div class="container">
  <h2>Vertical (basic) form</h2>
  <form >
    <div class="form-group">
      <label for="email">Email:</label>
      <input type="email" class="form-control" id="email" placeholder="Enter email" name="email">
	  <span>email</span>
    </div>
    <div class="form-group">
      <label for="pwd">Password:</label>
      <input type="password" class="form-control" id="pwd" placeholder="Enter password" name="pwd">
	  <span>password</span>
    </div>
    <div class="checkbox">
      <label><input type="checkbox" name="remember"> Remember me</label>
    </div>
    <button type="submit" class="btn btn-default btn1">Submit</button>
  </form>
</div>

</body>
</html>

Hi there toronto2009,

Why don’t you let HTML attribute required to do the heavy lifting…

 <input type="email" class="form-control" id="email" placeholder="Enter email" name="email" required>

 <input type="password" class="form-control" id="pwd" placeholder="Enter password" name="pwd" required>

coothead

Hi coothead
thank you for answer. having courses in php, required caused problem, so I decided to avoid it and find another way.

You’ve lost me there. :wonky:

coothead

I am sorry I don’t understand, frank

That makes two of us. :rofl:

coothead

JS isn’t my thing but I’m guessing you wanted $(this) in the above code rather than $('.form-control') because .form-control is many elements not just one of them!

Doesn’t bootstrap have its own validation plugin rather than writing your own?

As @coothead said above the required attribute is a lot less fuss than all this js lark :slight_smile:

1 Like

…and much more reliable. :biggrin:

coothead

1 Like

Hii coothead
I managed to do the floating label as I planed but placeholder needs to be moved right, can I get help, thank you frank

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Bootstrap Example</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
  <script>
  	$(document).ready(function(){
    
	  $("input").focusin(function(){
			//$(this).css("position","relative");
			$(this).css({"background-color": "lightgreen",
                         "border":"1px solid green",
                         "color": "red"
            });
            $(this).nextAll(".floatbase").css({"color":"red",
            									"bottom":"28px"});
           
			//$(this).addClass(".float");
		
	  }); 
	  $("input").focusout(function(){
			//$(".pwbb").css("position","relative");
			$(this).css({"background-color": "#FFFFFF",
					"border":"1px solid gray",
                    "color": "gray"
					});
			$(this).nextAll(".floatbase").css({"color":"gray",
            									"bottom":"2px"});

 }); 
 }); 
  </script>
  <style>
  .box{
  	position:relative;
    
    }
  .float{
  	bottom:28px;
    color:gray;
   }
  .floatbase{
  	 position:absolute;
     bottom:0px;
     left:5px;
   }
  </style>
</head>
<body>

<div class="container">
  <h2>Vertical (basic) form</h2>
  <form action="/action_page.php">
    <div class="form-group">
      <div class="box">
        <input type="email" class="form-control" id="email" placeholder="Like John.Doe@email.com" name="email">
        <label class="floatbase" for="email">Email:</label>
      </div>
    </div>
    <div class="form-group">
       <div class="box">
      <input type="password" class="form-control" id="pwd" placeholder=" Like Password123" name="pwd">
	   <label class="floatbase" for="pwd">Password:</label>
    </div>
	</div>
    <div class="checkbox">
      <label><input type="checkbox" name="remember"> Remember me</label>
    </div>
    <button type="submit" class="btn btn-default">Submit</button>
  </form>
</div>

</body>
</html>

Did you fix the other error?
i.e. this:

var inp = $(this).val().trim();

Not really sure why you would want to do this but the following code will move the placeholder to the right.

     input[type="text"]::placeholder,
    input[type="email"]::placeholder,
    input[type="password"]::placeholder {     text-align: right;           }

Be very careful with moving labels and placeholders as they generally end up upsetting the user in some way.

It sounds crease but with animation floating label floats up, placeholder goes transparent or pics up background color, thank you, frank

I have this code and I not really understand it, as I look it , the placeholder … I don’t understand

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Bootstrap Example</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
  <script>
  	$(document).ready(function(){
    
	  $("input").focusin(function(){
			//$(this).css("position","relative");
			$(this).css({"background-color": "lightgreen",
                         "border":"1px solid green",
                         "color": "red"
            });
            $(this).nextAll(".floatbase").css({"color":"green",
            									"bottom":"28px"});
           
			//$(this).addClass(".float");
		
	  }); 
	  $("input").focusout(function(){
			//$(".pwbb").css("position","relative");
			$(this).css({"background-color": "#FFFFFF",
					"border":"1px solid gray",
                    "color": "gray"
					});
			$(this).nextAll(".floatbase").css({"color":"gray",
            									"bottom":"2px"});

 }); 
 }); 
  </script>
  <style>
  .box{
  	position:relative;
    
    }
  .float{
  	bottom:28px;
    color:gray;
   }
  .floatbase{
  	 position:absolute;
     bottom:0px;
     left:5px;
   }
     input[type="text"]::placeholder,
    input[type="email"]::placeholder,
    input[type="password"]::-ms-input-placeholder {  
   padding-left: 100px;
   
	/*text-align: right; */
	padding-left: 100px;
	}
	::-webkit-input-placeholder {
   padding-left: 100px;
}
::-moz-placeholder { 
   padding-left: 100px;
}
:-ms-input-placeholder {  
   padding-left: 100px;
}
  </style>
</head>
<body>

<div class="container">
  <h2>Vertical (basic) form</h2>
  <form action="/action_page.php">
    <div class="form-group">
      <div class="box">
        <input type="email" class="form-control" id="email" placeholder="Like email" name="email">
        <label class="floatbase" for="email">Email:</label>
      </div>
    </div>
    <div class="form-group">
       <div class="box">
      <input type="password" class="form-control" id="pwd" placeholder="Enter password" name="pwd">
	   <label class="floatbase" for="pwd">Password:</label>
    </div>
	</div>
    <div class="checkbox">
      <label><input type="checkbox" name="remember"> Remember me</label>
    </div>
    <button type="submit" class="btn btn-default">Submit</button>
  </form>
</div>

</body>
</html>

It’s not clear which parts you don’t understand?

Are you still talking about the ::placeholder property. Note that padding is not a valid property for that pseudo element.

Only a subset of cs properties apply to the ::placeholder.

yes, this is he problem. I’ve got something but I don’t understand it. please help me to get it right, thank you, Frank
ps.: as you can see , the code does something but I feel is not the right thing.

If you can explain in more detail what is not right then I will be back tomorrow and take a look.

To me it all looks wrong so you will need to point me exactly to the problem you want me to look at.:slight_smile:

If its just a floating placeholder then I did one here a while back but I would not use it myself :slight_smile:

yes the placeholder looks not right. I try to move right 118px, also I try to ad glyph icons from my previous experiments… thank you, frank.
ps,: I try to make placeholder to become transparent or “white” gradualy with animation meanwhile floating label floats up on clicking on a chosen input.

As I said only a subset of css properties are valid for the placeholder although browsers may support others.

You can probably move the placeholder with text-indent but I haven’t tested.

You can’t however address pseudo elements like the ::placeholder in JS because they are not part of the DOM.

Instead you dynamically add a class to a parent of the control and then use that class in css to make changes to your elements including any placeholders.

Generally avoid directly styling things with js but more easily add and remove classes with the js and then handle the styling from css where it belongs. :slight_smile:

Back tomorrow.

1 Like

thank you for answer. As an option I was thinking on b to float

I don’t think you can do that as the placeholder disappears as soon as you type and is handled automatically by the browser.

If you want that feature you will probably have to write your own placeholder. You can probably just add it to your current floating label as a nested span and treat it as a placeholder with css. Position the element next to the floating text but don’t move it up with the rest and then fade it out wit css in the usual way

yes but I don’t have no idea at the moment how to make it