<link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/wing.min.css">
<div class="container">
<h1>
Vanilla Javascript Component (ES5)
</h1>
<p>
This is a demonstration of writing clean Javascript - by adopting a component based approach.
</p>
<p>
No jQuery, no poorly structured spaghetti code, composed of single purpose functions, has no hard coded selectors and works in IE10+.
</p>
<p>
This is written in ES5. If build tools were used I would write this in ES7+.
If Vue.js was used as well then a good chunk of the code wouldn't be necessary (and no need to attach to window).
</p>
<hr />
<h5>
Password Confirmation Component
</h5>
<div class="row">
<div class="col">
<label>Password</label>
<input id="password-input" type="password">
</div>
<div class="col">
<label>Confirm Password</label>
<input id="password-confirm-input" type="password">
</div>
</div>
<div class="row">
<div class="col">
<div id="password-validation-match">
Passwords must match
</div>
<div id="password-validation-length">
Password must be at least 8 characters
</div>
<div id="password-validation-numeric">
Password must contain numeric values
</div>
<div id="password-validation-upper">
Password must contain uppercase characters
</div>
<div id="password-validation-lower">
Password must contain lowercase characters
</div>
</div>
</div>
<div class="row">
<button id="activate-account-submit">
Confirm
</button>
</div>
</div>
JavaScript
// This is a demonstration of writing clean Javascript - by adopting a component based approach.
// No jQuery, no poorly structured spaghetti code, composed of single purpose functions, has no hard coded selectors and works in IE10+.
// This is written in ES5. If build tools were used I would write this in ES7+.
// If Vue.js was used as well then a good chunk of the code wouldn't be necessary (and no need to attach to window).
window.passwordConfirmationComponent = {
selectors: {},
updatePasswordRuleFeedback: function (validation) {
//TODO: Refactor to be DRY
if (!validation.match) {
this.selectors.passwordValidationMatch.classList.remove('hidden');
} else {
this.selectors.passwordValidationMatch.classList.add('hidden');
}
if (!validation.length) {
this.selectors.passwordValidationLength.classList.remove('hidden');
} else {
this.selectors.passwordValidationLength.classList.add('hidden');
}
if (!validation.numeric) {
this.selectors.passwordValidationNumeric.classList.remove('hidden');
} else {
this.selectors.passwordValidationNumeric.classList.add('hidden');
}
if (!validation.lower) {
this.selectors.passwordValidationLower.classList.remove('hidden');
} else {
this.selectors.passwordValidationLower.classList.add('hidden');
}
if (!validation.upper) {
this.selectors.passwordValidationUpper.classList.remove('hidden');
} else {
this.selectors.passwordValidationUpper.classList.add('hidden');
}
},
enableSubmitButton: function enableSubmitButton() {
this.selectors.submitButton.removeAttribute('disabled');
},
disableSubmitButton: function disableSubmitButton() {
this.selectors.submitButton.setAttribute('disabled', true);
},
passwordsUpdated: function passwordsUpdated() {
...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.