JSFiddle - React, Tailwind, and code Playground

Allyse Groover - Username and Password

by David Lomneck

HTML

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/semantic-ui/2.2.4/semantic.min.css">
<script src="https://getfirebug.com/firebug-lite-debug.js"></script>
<div class="ui container">
  <div class="ui form error">
    <div class="field error">
      <label>Username</label>
      <input type="text" placeholder="Username" id="inputUsername">
    </div>
    <div class="field error">
      <label>Password</label>
      <input type="text" placeholder="Password" id="inputPassword">
    </div>
    <div class="ui error message">
      <div class="header">Something is invalid...</div>
      <p id="errorMessage"></p>
    </div>
  </div>
</div>

JavaScript

//
// Complete implementations of the validateUsername and validatePassword functions
// throwing errors when invalid. You may use: https://developer.mozilla.org if you need help with syntax
// https://getfirebug.com/firebug-lite-debug.js as external resource for a build-in console window


// A valid username is at least 6 characters long
// A valid username cannot be longer than 20 characters
function validateUsername(username) {
		if( username.length >= 6 && username.length <= 20 ) {
    	return username;
    } else {
  		throw 'Username is not valid!';
    }
}


// A valid password is at least 8 characters long
// A valid password cannot be longer 30 characters
// A valid password must contain at least 1 UPPER case letter and 1 lower case letter
// A valid password must contain at least 1 number
// A valid password must contain at least one of the following special symbols: !@#$%^&*(){}[]?<>.,
function validatePassword(password) {
	var numberStroke = password.search(/\d/);
  var capital = password.search(/[A-Z]/);
  var lower = password.search(/[a-z]/);
  var specialChar = password.search(/[\!\@\#\$\%\^\&\*\(\)\{\}\[\]\?\<\>\,\.]/)
		if( password.length >= 8 && password.length <= 30 ) {
    	if (numberStroke >= 0 && capital >= 0 && lower >= 0 && specialChar >= 0 ) {
      	return password;
      } else {
      	throw 'Password is not valid!';
      }
    } else {
  		throw 'Password is not valid!';
    }
}





// You can ignore everything below this comment
(function() {
  var elements = {
    username: document.getElementById('inputUsername'),
    password: document.getElementById('inputPassword'),
    errorMessage: document.getElementById('errorMessage'),
    form: document.getElementsByClassName('ui form')[0]
  };
  var errors = {};

  wireup(elements.username, validateUsername);
  wireup(elements.password, validatePassword);

  function wireup(element, callback) {
    element.addEventListener('input', function(e) {
      try {
       ...