I'm supposed to create a website that adds multiple forms to the page, is responsive and checks if the inputs are valid (validation is not important, just needs to show some attempt at regex). Below is what I've written so far. What I'm looking for is any advice on making it more efficient and compact. Any and all help is appreciated and considered. Thanks in advance!
index.html
<!DOCTYPE html>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<title>index.html</title>
<script src="script.js"></script>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body onload="execute()" id="body">
<h3>Content Below:</h3>
<div id="buffer">
<div id="content">
<!-- Content will go here -->
</div>
</div>
<div id="info">
<!-- Info will go here -->
</div>
</body>
</html>
script.js
function ContentDisplayer() {
this.count = 0;
this.show = function(id) {
var tag = document.getElementById(id);
tag.style.display = 'block';
}
this.showText = function(id, content) {
var tag = document.getElementById(id);
tag.innerHTML = content;
}
this.constructForm = function(containing_id, question) {
//Create div containing the form
var div_tag = document.createElement('div');
div_tag.id = 'div_' + this.count;
document.getElementById('body').appendChild(div_tag);
//Create the form tag
var form_tag = document.createElement('form');
form_tag.id = 'form_' + this.count;
document.getElementById(div_tag.id).appendChild(form_tag);
//Create the fieldset tag
var fieldset_tag = document.createElement('fieldset');
fieldset_tag.id = 'fieldset_' + this.count;
document.getElementById(form_tag.id).appendChild(fieldset_tag);
//Create question label
var label_tag = document.createElement('label');
var label_text = document.createTextNode(question);
label_tag.appendChild(label_text);
label_tag.id = 'label_' + this.count;
document.getElementById(fieldset_tag.id).appendChild(label_tag);
//insert line break
var break_tag = document.createElement('br');
document.getElementById(fieldset_tag.id).appendChild(break_tag);
//Create answer label
var input_tag = document.createElement('input');
input_tag.id = 'input_' + this.count;
input_tag.type = 'text';
document.getElementById(fieldset_tag.id).appendChild(input_tag);
//insert line break
var break_tag = document.createElement('br');
document.getElementById(fieldset_tag.id).appendChild(break_tag);
//Create button
var button_tag = document.createElement('button');
var button_text = document.createTextNode('Submit');
button_tag.appendChild(button_text);
button_tag.type = 'button';
button_tag.id = 'button_' + this.count;
button_tag.onclick = function() {
var x = document.getElementById(input_tag.id);
if(input_tag.id == 'input_0') {
if(/^[a-zA-Z]+$/.test(x.value)) {
x.style.backgroundColor = "green";
x.style.borderColor = "green";
}
}
if(input_tag.id == 'input_1') {
if((/^[0-9]+$/.test(x.value)) && x.value > 0 && x.value <= 100) {
x.style.backgroundColor = "green";
x.style.borderColor = "green";
}
}
if(input_tag.id == 'input_2') {
if(/^\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,3}$/.test(x.value)) {
x.style.backgroundColor = "green";
x.style.borderColor = "green";
}
}
if(input_tag.id == 'input_3') {
if(/\d{1,5}\s\w{1,10}\s\w{1,10}/.test(x.value)) {
x.style.backgroundColor = "green";
x.style.borderColor = "green";
}
}
if(input_tag.id == 'input_4') {
if(/^\d{3}-\d{3}-\d{4}$/.test(x.value)) {
x.style.backgroundColor = "green";
x.style.borderColor = "green";
}
}
}
document.getElementById(fieldset_tag.id).appendChild(button_tag);
this.count += 1;
}
}
var c;
var questions = [
'Enter your first name',
'Enter your age',
'Enter your email',
'Enter your address',
'Enter your phone number (must use dashes): ###-###-####'
];
var question_ids = [
'name_content',
'age_content',
'email_content',
'address_content',
'phone_content'
];
function execute() {
c = new ContentDisplayer();
c.show('buffer');
c.showText('content', '<h1>Hello!</h1>');
c.showText('info', 'If the box turns green, the information is valid!');
//create loop to add forms to page
for (var i = 0; i < questions.length; i++) {
c.constructForm(question_ids[i], questions[i]);
}
}
style.css
body {
font-family: "Times New Roman", Times, serif;
background-color: pink;
}
.buffer {
display: none;
}
input[type=text] {
border: 2px solid red;
border-radius: 4px;
background-color: #f44242;
margin: 1px;
}
input[type=text]:focus {
border: 2px solid blue;
border-radius: 4px;
background-color: #41dcf4;
}
button {
background-color: green;
border: none;
border-radius: 6px;
color: white;
text-align: center;
font-size: 16px;
}
jsdom
. \$\endgroup\$