0

This is my HTML form:

<form action="process.php" id="login" method="POST">
<p>Username: <?php echo $form->error("user"); ?></p>
<div>
<input type="text" style="font-size: 13px; color: #7e7e7e;" autocomplete="off" id="user" name="user" maxlength="18" length="18" value="<?php echo $form->value("user"); ?>">
</div>
<p>Password: <?php echo $form->error("pass"); ?></p>
<input type="password" name="pass" id="pass" value="<?php echo $form->value("pass"); ?>">
<p>Email:</p>
<?php echo $form->error("email"); ?>
<input type="text" name="email" value="<?php echo $form->value("email"); ?>">
<input type="hidden" name="fav_continent" value="Default">
<input type="hidden" name="fav_colour" value="Default">
<input type="hidden" name="subjoin" value="1">
<input type="submit" value="Register">
</form>

Here's my custom style for the text input field:

input[type=text], input[type=password], input[type=email] {
  background-color: transparent;
  border: none;
  border-bottom: 1px solid #9e9e9e;
  border-radius: 0;
  outline: none;
  height: 3rem;
  width: 100%;
  font-size: 1rem;
  margin: 0 0 15px 0;
  padding: 0;
  box-shadow: none;
  -webkit-box-sizing: content-box;
  -moz-box-sizing: content-box;
  box-sizing: content-box;
  transition: all .3s; 
}

It looks like this in JSFIDDLE: http://jsfiddle.net/qzrtccLx/

If a user submits the form and either one of the text input fields is empty, how do I make the EMPTY text input field's change from border-bottom: 1px solid #9e9e9e; to border-bottom: 1px solid red;?

2
  • You write some javascript. You've attempted to write no code. This is easy. Commented Sep 10, 2015 at 20:15
  • I mean easy in the sense of easy to look up on Google. Or even on SO Commented Sep 10, 2015 at 20:17

5 Answers 5

2

Using the required attribute we can use the :invalid pseudo class. The Snippet won't demonstrate this behaviour with PHP so I have emitted it all.

input[type=text], input[type=password], input[type=email] {
  background-color: transparent;
  border: none;
  border-bottom: 1px solid #9e9e9e;
  border-radius: 0;
  outline: none;
  height: 3rem;
  width: 100%;
  font-size: 1rem;
  margin: 0 0 15px 0;
  padding: 0;
  box-shadow: none;
  -webkit-box-sizing: content-box;
  -moz-box-sizing: content-box;
  box-sizing: content-box;
  transition: all .3s; 
}
input[type=text]:invalid {
  border-color:red;
}
<form action="process.php" id="login" method="POST">
<p>Username: </p>
<div>
<input type="text" style="font-size: 13px; color: #7e7e7e;" autocomplete="off" id="user" name="user" maxlength="18" length="18" value="" required>
</div>
<p>Password: </p>
<input type="password" name="pass" id="pass" value="">
<p>Email:</p>
<input type="text" name="email" value="">
<input type="hidden" name="fav_continent" value="Default">
<input type="hidden" name="fav_colour" value="Default">
<input type="hidden" name="subjoin" value="1">
<input type="submit" value="Register">
</form>

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

Comments

1

Here's a answer in pure JavaScript. I've written an event to check all the inputs when the form is submitted. If there are any values that are empty, then I update the class on the input and prevent the form from submitting.

var form = document.getElementById("login");
login.addEventListener("submit", function(e) {
  var valid = true;
  var inputs = login.querySelectorAll("input");
  [].forEach.call(inputs, function(input) {
    if (input.value === "") {
      valid = false;
      input.classList.add("error");
    }  
  });
  
  if (!valid)
    e.preventDefault();
});
input[type=text], input[type=password], input[type=email] {
  background-color: transparent;
  border: none;
  border-bottom: 1px solid #9e9e9e;
  border-radius: 0;
  outline: none;
  height: 3rem;
  width: 100%;
  font-size: 1rem;
  margin: 0 0 15px 0;
  padding: 0;
  box-shadow: none;
  -webkit-box-sizing: content-box;
  -moz-box-sizing: content-box;
  box-sizing: content-box;
  transition: all .3s; 
}

input[type=text]:focus:not([readonly]), input[type=password]:focus:not([readonly]), input[type=email]:focus:not([readonly]) {
 border-bottom: 1px solid #26a69a;
 box-shadow: 0 1px 0 0 #26a69a; 
}

input[type=text].error {
  border-bottom: 1px solid red;
}
<form action="process.php" id="login" method="POST">
  <p>Username: </p>
  <div>
    <input type="text" style="font-size: 13px; color: #7e7e7e;" autocomplete="off" id="user" name="user" maxlength="18" length="18">
  </div>
  <p>Password:</p>
  <input type="password" name="pass" id="pass">
  <p>Email:</p>
  <input type="text" name="email">
  <input type="hidden" name="fav_continent" value="Default">
  <input type="hidden" name="fav_colour" value="Default">
  <input type="hidden" name="subjoin" value="1">
  <input type="submit" value="Register">
</form>

Comments

1

If you're open to using jQuery, you should use jQuery's submit() function to handle the form submit. This function also handles the actual form submission. return true; to submit and return false; to halt submission.

$("#login").submit(function() {
    $(this).find("input[type='text']").each(function() {
        if (!$(this).val()) {
             $(this).css("border-bottom", "1px solid red");
        }
    }
}

3 Comments

He's not using jQuery.
For one reason or another, this isn't working - the border-bottom isn't changing to red..
@IainProctor Did you include jquery in your header?
0

You need to use JS to intercept the form's submission by attaching an event listener.

var form = document.getElementById('login');
form.addEventListener('submit', function(e){
    //Code here
}, false);

Then inside the function, find all the input fields and loop through each one.

var inputs = form.getElementsByTagName('input');
for(var i=0;i<inputs.length;i++){
    //Check each input value individually
}

Then inside the loop, check if the value of the input is empty. If so, Give it a red border and cancel the form submission.

if(inputs[i].value=='') {
    inputs[i].style.borderBottom = '1px solid red';
    e.preventDefault();
}

Take a look at this updated fiddle

Comments

-1

Use jquery .val() and a submit event handler

For example

$( "form" ).submit(function( event ) {
   if($('input').val() == ''){
        $('input').css('border-bottom:', '1px solid red');
   }
})

1 Comment

It is a suggestion, not the entire answer.

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.