JavaScript preprocessors can help make authoring JavaScript easier and more convenient. For instance, CoffeeScript can help prevent easy-to-make mistakes and offer a cleaner syntax and Babel can bring ECMAScript 6 features to browsers that only support ECMAScript 5.
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.
You can apply a script from anywhere on the web to your Pen. Just put a URL to it here and we'll add it, in the order you have them, before the JavaScript in the Pen itself.
If the script you link to has the file extension of a preprocessor, we'll attempt to process it before applying.
You can also link to another Pen here, and we'll pull the JavaScript from that Pen and include it. If it's using a matching preprocessor, we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
HTML Settings
Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.
<form name="myform">
<label for="name">Name</label>
<input name="name" type="text" onchange="validateOneInput(this)"/>
<p id="errorMessage-name"></p>
<label for="email">Email</label>
<input name="email" type="email" onchange="validateOneInput(this)"/>
<p id="errorMessage-email"></p>
<label for="website">Website</label>
<input name="website" type="url" onchange="validateOneInput(this)"/>
<p id="errorMessage-website"></p>
<input type="button" value="submit" name="submit" onclick="submitForm()"/>
</form>
[id^="errorMessage"] {
min-height:1em;
color: red;
font-size: 0.7rem;
margin: 0 1rem;
}
input, p, label {
font-size: 1rem;
font-family: Helvetica, sans-serif;
}
input {
box-sizing: border-box;
padding: 0.5rem;
margin: 1rem 1rem 0 1rem;
width:300px;
}
p {
width: 500px;
margin-top:0.5rem;
}
label {
display: block;
margin-top:1.5rem;
}
function clearError(el) {
document.getElementById("errorMessage-" + el.name).innerHTML = "";
}
function validateOneInput(el) {
clearError(el);
if (!el.value) {
// display each error under input field
document.getElementById("errorMessage-" + el.name).innerHTML =
'The "' + el.name + '" field is empty';
return false;
} else if (el.name == "email" && el.value.indexOf("@") < 1) {
// check for example if email field contains @ character
document.getElementById("errorMessage-email").innerHTML =
'The "email" field is invalid';
return false;
}
return true;
}
function validate(el) {
const form = document.forms["myform"];
// add flag for error messages
let validated = true;
// check each of the field if any of them is empty
for (let i = 0; i < form.length; i++) {
// check individually all form elements except for submit input
if (form[i].name !== "submit" && !validateOneInput(form[i])) {
validated = false;
}
}
return validated;
}
function submitForm() {
if (validate()) {
window.alert("form can be submitted");
} else {
window.alert("form can not be submitted");
}
}
Also see: Tab Triggers