Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div class="contenedor-formulario">
  <div class="wrap">
    <form action="" class="formulario" name="formulario_registro" method="get">
      <div>
        <div class="input-group">
          <input type="text" id="nombre" name="nombre">
          <label class="label" for="nombre">Nombre:</label>
        </div>
        <div class="input-group">
          <input type="email" id="correo" name="correo">
          <label class="label" for="correo">Correo:</label>
        </div>
        <div class="input-group">
          <input type="password" id="pass" name="pass">
          <label class="label" for="pass">Contraseña:</label>
        </div>
        <div class="input-group">
          <input type="password" id="pass2" name="pass2">
          <label class="label" for="pass2">Repetir Contraseña:</label>
        </div>
        <div class="input-group radio">
          <input type="radio" name="sexo" id="hombre" value="Hombre">
          <label for="hombre">Hombre</label>
          <input type="radio" name="sexo" id="mujer" value="Mujer">
          <label for="mujer">Mujer</label>
        </div>
        <div class="input-group checkbox">
          <input type="checkbox" name="terminos" id="terminos" value="true">
          <label for="terminos">Acepto los Terminos y Condiciones</label>
        </div>

        <input type="submit" id="btn-submit" value="Enviar">
      </div>
    </form>
  </div>
</div>
              
            
!

CSS

              
                $primary: #303F9F
$light-primary: #BBDEFB
$white: #f2f2f2
$divider: #B6B6B6
$error: #D32F2F

$borde: 2px
$font-family: "Roboto", Arial

*
	margin: 0
	padding: 0
	-webkit-box-sizing: border-box
	-moz-box-sizing: border-box
	box-sizing: border-box

body
	background: $white
	font-family: $font-family
	border-top: 30px solid $primary

.wrap
	width: 90%
	max-width: 500px
	padding: 40px
	margin: auto
	background: #fff
	box-shadow: 0px 0px 3px grey

.contenedor-formulario
	width: 100%
	color: $white
	padding: 50px
	.formulario
		width: 100%
		margin: auto
		.input-group
			position: relative
			margin-bottom: 32px
			// background: rgba(#ccc, .2)
			input[type="text"],
			input[type="email"],
			input[type="password"],
				font-family: $font-family
				font-size: 16px
				color: $primary
				width: 100%
				outline: none
				padding: 15px
				background: none
				border: none
				border-bottom: $borde solid $light-primary
				&:focus,
				&:active
					outline: none
					border-bottom: $borde solid $primary
				&.error
					border-bottom: $borde solid $error
					+ label
						color: $error
			label
				color: $primary
				&.label
					-webkit-transition: all .3s ease
					-o-transition: all .3s ease
					transition: all .3s ease
					margin-left: 15px
					font-size: 16px
					line-height: 16px
					position: absolute
					top: 16px
					left: 0
					&.active
						top: -12px
						font-size: 12px
						line-height: 12px
						color: $divider
			&.checkbox label,
			&.radio label
				display: inline-block
				cursor: pointer
				color: $primary
				position: relative
				padding: 5px 15px 5px 51px
				font-size: 1em
				border-radius: 3px
				-webkit-transition: all .3s ease
				-o-transition: all .3s ease
				transition: all .3s ease
				&:hover
					background: rgba($light-primary, 1)
				&:before
					content: ""
					display: inline-block
					width: 17px
					height: 17px
					position: absolute
					left: 15px
					border-radius: 50%
					background: none
					border: $borde solid $primary
			// Clase de error para Checkbox y Radio
			&.error
				label
					color: $error
					&:hover
						background: rgba($error, .2)
					&:before
						border: $borde solid $error
			&.radio
				input[type="radio"]
					display: none
					&:checked + label:before
						display: none
					&:checked + label
						padding: 5px 15px
						background: $primary
						border-radius: $borde
						color: $white
			&.checkbox
				label:before 
					border-radius: 3px
				input[type="checkbox"]
					display: none
					&:checked + label:before 
						display: none
					&:checked + label
						background: $primary
						color: $white
						padding: 5px 15px
		input[type="submit"]
			background: $primary
			border-radius: 1px
			border: $borde solid $white
			color: #fff
			cursor: pointer
			display: inline-block
			font-family: $font-family
			font-size: 16px
			padding: 15px
			width: 100%
			-webkit-transition: all .3s ease
			-o-transition: all .3s ease
			transition: all .3s ease
			&:hover
				background: #1976D2
              
            
!

JS

              
                // Recorrer los elementos y hacer que onchange ejecute una funcion para comprobar el valor de ese input
(function() {

  var formulario = document.formulario_registro,
    elementos = formulario.elements;

  // Funcion que se ejecuta cuando el evento click es activado

  var validarInputs = function() {
    for (var i = 0; i < elementos.length; i++) {
      // Identificamos si el elemento es de tipo texto, email, password, radio o checkbox
      if (elementos[i].type == "text" || elementos[i].type == "email" || elementos[i].type == "password") {
        // Si es tipo texto, email o password vamos a comprobar que esten completados los input
        if (elementos[i].value.length == 0) {
          console.log('El campo ' + elementos[i].name + ' esta incompleto');
          elementos[i].className = elementos[i].className + " error";
          return false;
        } else {
          elementos[i].className = elementos[i].className.replace(" error", "");
        }
      }
    }

    // Comprobando que las contraseñas coincidan
    if (elementos.pass.value !== elementos.pass2.value) {
      elementos.pass.value = "";
      elementos.pass2.value = "";
      elementos.pass.className = elementos.pass.className + " error";
      elementos.pass2.className = elementos.pass2.className + " error";
    } else {
      elementos.pass.className = elementos.pass.className.replace(" error", "");
      elementos.pass2.className = elementos.pass2.className.replace(" error", "");
    }

    return true;
  };

  var validarRadios = function() {
    var opciones = document.getElementsByName('sexo'),
      resultado = false;

    for (var i = 0; i < elementos.length; i++) {
      if (elementos[i].type == "radio" && elementos[i].name == "sexo") {
        // Recorremos los radio button
        for (var o = 0; o < opciones.length; o++) {
          if (opciones[o].checked) {
            resultado = true;
            break;
          }
        }

        if (resultado == false) {
          elementos[i].parentNode.className = elementos[i].parentNode.className + " error";
          console.log('El campo sexo esta incompleto');
          return false;
        } else {
          // Eliminamos la clase Error del radio button
          elementos[i].parentNode.className = elementos[i].parentNode.className.replace(" error", "");
          return true;
        }
      }
    }
  };

  var validarCheckbox = function() {
    var opciones = document.getElementsByName('terminos'),
      resultado = false;

    for (var i = 0; i < elementos.length; i++) {
      if (elementos[i].type == "checkbox") {
        for (var o = 0; o < opciones.length; o++) {
          if (opciones[o].checked) {
            resultado = true;
            break;
          }
        }

        if (resultado == false) {
          elementos[i].parentNode.className = elementos[i].parentNode.className + " error";
          console.log('El campo checkbox esta incompleto');
          return false;
        } else {
          // Eliminamos la clase Error del checkbox
          elementos[i].parentNode.className = elementos[i].parentNode.className.replace(" error", "");
          return true;
        }
      }
    }
  };

  var enviar = function(e) {
    if (!validarInputs()) {
      console.log('Falto validar los Input');
      e.preventDefault();
    } else if (!validarRadios()) {
      console.log('Falto validar los Radio Button');
      e.preventDefault();
    } else if (!validarCheckbox()) {
      console.log('Falto validar Checkbox');
      e.preventDefault();
    } else {
      console.log('Envia');
      e.preventDefault();
    }
  };

  var focusInput = function() {
    this.parentElement.children[1].className = "label active";
    this.parentElement.children[0].className = this.parentElement.children[0].className.replace("error", "");
  };

  var blurInput = function() {
    if (this.value <= 0) {
      this.parentElement.children[1].className = "label";
      this.parentElement.children[0].className = this.parentElement.children[0].className + " error";
    }
  };

  // --- Eventos ---
  formulario.addEventListener("submit", enviar);

  for (var i = 0; i < elementos.length; i++) {
    if (elementos[i].type == "text" || elementos[i].type == "email" || elementos[i].type == "password") {
      elementos[i].addEventListener("focus", focusInput);
      elementos[i].addEventListener("blur", blurInput);
    }
  }

}())
              
            
!
999px

Console