Use HTML To Send and Receive Validated Forms From Home Computer


I previously posted a form with pre-submission verification. This app extends that to allow posting of the form data.

Until the form is properly filled, it will not be posted.

This could be used to send questions to an email list directly from a local computer, without the need for a server.

Here is the output as viewed in an email client:

Here is the code:

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”&gt;
<html xmlns=”http://www.w3.org/1999/xhtml”&gt;
<head profile=”http://gmpg.org/xfn/11″&gt;
<title>HTML5 Validation</title>
<style>
body {margin-left:0;margin-right:0;font:normal normal normal 15px Arial; background: #666666}
a{ text-decoration: }:link { color: rgb(0, 0, 255) }:visited {color :rgb(100, 0,100) }:hover { }:active { }
form{position: absolute; width: 200px; left: 50%; margin-left: -100px;}
input{font:normal normal 700 20px Arial; border: 4px ridge}
</style>
</head>
<body>
<form action=”HTML5b.html” method=”post” >
<input type=”text” id=”t1″ name=”t1″ value=”” placeholder=”subject” required /><br />
<input type=”date” id=”date1″ name=”date1″ required /><br />
<input type=”tel” id=”tel1″ name=”tel1″ value=”” placeholder = “phone” required /><br />
<input type=”email” id=”e1″ name=”e1″ value=”” placeholder = “email
” required/><br />
<input type=”url” id=”u1″ name=”u1″ value=”” placeholder = “url” required /><br />
<input type=”number” id=”n1″ name=”n1″ value=”” placeholder = “number” /><br />
<button type=”submit” id=”submit”style = “font:normal normal 700 20px Arial” name=”submit” value=”submit” onclick=’sendit();’ >Submit</button>
</form>

function sendit() {
if (document.getElementById(“n1”).value != “”) {
var subject = document.getElementById(“t1”).value;
var body2 = ‘text: ‘ + document.getElementById(“t1”).value + ‘%0D%0Adate: ‘ + document.getElementById(“date1”).value + ‘%0D%0Aphone: ‘ + document.getElementById(“tel1”).value + ‘%0D%0Aemail: ‘ + document.getElementById(“e1”).value + ‘%0D%0Aurl: ‘ + document.getElementById(“u1”).value + ‘%0D%0Anumber: ‘ + document.getElementById(“n1”).value;
window.open(‘mailto: email@host.com?subject=’ + subject + ‘&body=’ + body2);
}
}

</body></html>


The script consists of a function named sendit() which contains the mailto command:
window.open(‘mailto: email@host.com?subject=’ + subject + ‘&body=’ + body2);

The mailto part of the string is of course the recipient email;

An event listener to call this function is placed in the submit button:
<button type=”submit” id=”submit”style = “font:normal normal 700 20px Arial” name=”submit” value=”submit” onclick=’sendit();’

Since this precedes the form submission, it is necessary to be certain all fields have been verified first. This is done by making the function call dependent on the verification of the last field:
if (document.getElementById(“n1”).value != “”) {

New Ways to Set Font Size


With html, there are very few font sizes allowed. CSS expands that so fonts can be set by px pt and em.

It would be good however to set the font in more familiar sizes. The first app here sets the size in millimeters while the second
sets it in percentages of the screen width, the way css allows that for elements.

Here is the code for the first app:

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”&gt;
<html xmlns=”http://www.w3.org/1999/xhtml”&gt;
<head profile=”http://gmpg.org/xfn/11″&gt;
<title>HTML Editor</title>
<style>
body {margin-left:0;margin-right:0;font:normal normal normal 15px Arial;}
a{ text-decoration: }:link { color: rgb(0, 0, 255) }:visited {color :rgb(100, 0,100) }:hover { }:active { }

</style>
</head>
<body>
<input type=”text” id=”t1″style = position: absolute; top 0; name=”t1″ value=”” placeholder=”font size in mm” ondblclick = ‘rsize();’/>

<di id=”d1″ style=”position: absolute; top: 10%; width: 100%; height: 90%; font : italic normal normal 18pt Georgia; background: #dddddd”>
This is a test
</di>

<scrpt type=”text/javascript”>
document.getElementById(“t1”).focus();
function rsize() {
var mms2pts = Math.round((72 * parseFloat(document.getElementById(“t1”).value)) / 25.4) + “pt”;
document.getElementById(“d1”).style.fontSize = mms2pts;
document.getElementById(“t1”).value = “”;
}
</scrpt>
</body></html>


The fist app has only a text input to enter the font size in mm and a display layer.
on loading the focus is put on the text input:
document.getElementById(“t1”).focus();

The function rsize() is called by double clicking the text input.

The size is reset from mm to points and the layer is given the new point size:
var mms2pts = Math.round((72 * parseFloat(document.getElementById(“t1”).value)) / 25.4) + “pt”;
document.getElementById(“d1”).style.fontSize = mms2pts;

Here is the code for the second app:

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”&gt;
<html xmlns=”http://www.w3.org/1999/xhtml”&gt;
<head profile=”http://gmpg.org/xfn/11″&gt;
<title>HTML Editor</title>
<style>
body {margin-left:0;margin-right:0;font:normal normal normal 15px Arial;}
a{ text-decoration: }:link { color: rgb(0, 0, 255) }:visited {color :rgb(100, 0,100) }:hover { }:active { }

</style>
</head>
<body>
<input type=”text” id=”t1″style = position: absolute; top 0; left: 10px; name=”t1″ value=”” placeholder=”font size in %” />
<select id=”s1″ name=”s1″ onchange=’rsize();’ >
<option ></option>
<option >Arial</option>
<option >Arial Black</option>
<option >Times new Roman</option>
<option >Georgia</option>
<option >Verdana</option>
<option >Comic Sans MS</option>
</select>
<input type=”text” id=”t2″ style = ” width: 900px”name=”t2″ value=”” placeholder=”enter text” />
<di id=”d1″ style=”position: absolute; top: 10%; left: 0; width: 100%; height: 90%; font : normal normal normal 18pt Arial; background: #dddddd”>
abcdefghij
</di>

<scrpt type=”text/javascript”>

var mul;
document.getElementById(“t1”).focus();
function rsize() {
mul = 9.5;
if (document.getElementById(“s1”).value.indexOf(“Black”) > -1) {
mul /= 11;
document.getElementById(“d1”).style.fontFamily = “Arial Black”;
}
if (document.getElementById(“s1”).value.indexOf(“Comic”) > -1) {
mul /= 9.5;
document.getElementById(“d1”).style.fontFamily = “Comic Sans MS”;
}
if (document.getElementById(“s1”).value.indexOf(“Times”) > -1) {
mul /= 8.3;
document.getElementById(“d1”).style.fontFamily = “Times New Roman”;
}
if (document.getElementById(“s1”).value == “Arial”) {
mul /= 8.8;
document.getElementById(“d1”).style.fontFamily = “Arial”;
}
if (document.getElementById(“s1”).value == “Verdana”) {
mul /= 10.1;
document.getElementById(“d1”).style.fontFamily = “Verdana”;
}
if (document.getElementById(“s1”).value == “Georgia”) {
mul /= 9.0;
document.getElementById(“d1”).style.fontFamily = “Georgia”;
}
var pc = Math.round(mul*(screen.width/50) * parseFloat(document.getElementById(“t1”).value)) ;

if (document.getElementById(“t2”).value != “” ) document.getElementById(“d1”).innerHTML = document.getElementById(“t2”).value;
document.getElementById(“d1”).style.fontSize = pc + “px”;

document.getElementById(“t1”).value = “”;
document.getElementById(“s1”).value = “”;
document.getElementById(“t1”).focus();
}
</scrpt>
</body></html>


The different fonts have to be normalized:
mul = 9.5;
if (document.getElementById(“s1”).value.indexOf(“Black”) > -1) {
mul /= 11;
document.getElementById(“d1”).style.fontFamily = “Arial Black”;
}

The font-size is calculated and set and the text is set:
var pc = Math.round(mul*(screen.width/50) * parseFloat(document.getElementById(“t1”).value)) ;

if (document.getElementById(“t2”).value != “” ) document.getElementById(“d1”).innerHTML = document.getElementById(“t2”).value;
document.getElementById(“d1”).style.fontSize = pc + “px”;

A HTML Gradient Generator


This app interactively generates CSS linear or radial gradients and lists the code, so the code can be pasted into a web page. CSS gradients are not as versatile as SVG ones, but are fine for simple gradients.

The Control Panel has two color inputs, each of which immediately updates the gradient on change.

Here is a typical appearance:

The linear gradient can be horizontal rather than vertical:

Of course, the linear and radial gradients are not required to have the same colors.

In the Control Panel, the left text box contains the gradient code, followed by the two color inputs.

The first checkbox, if checked, gives a horizontal gradient,

while the second checkbox if checked creates a radial gradient.

The last two text inputs set the stops for the gradient colors.

To use it click here

Here is the code:

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”&gt;
<html xmlns=”http://www.w3.org/1999/xhtml”&gt;
<head profile=”http://gmpg.org/xfn/11″&gt;
<title>Gradient Generator</title>
<style>
body {margin-left:0;margin-right:0;font:normal normal normal 15px Arial;}
a{ text-decoration: }:link { color: rgb(0, 0, 255) }:visited {color :rgb(100, 0,100) }:hover { }:active { }

</style>
</head>
<body>
<input type=”text” id=”t1″ name=”t1″ value=”” style=”width: 800px” /> <input type=’color’ id=’color1′ value= simple color onchange=’gradGen();’ /> <input type=’color’ id=’color2′ value= simple color onchange=’gradGen();’ /> <input type=”checkbox” id=”ch1″ style=”width: 15px” name=”ch1″ value=” ” />Horizontal <input type=”checkbox” id=”ch2″ style=”width: 15px” name=”ch2″ value=” ” />Radial <input type=”text” id=”t2″ name=”t2″ value=”1%” style=”width: 40px” /> <input type=”text” id=”t3″ name=”t3″ value=”85%” style=”width: 40px” />
<di id=”dlin” style=”position: absolute; top: 10%; left: 20px; width: 500px; height: 500px; border: 2px solid black;
background-image: -moz-linear-gradient(top, #ffffff, #ffffff);
background-image: -webkit-gradient(linear,left bottom,left top,color-stop(0, #ffffff),color-stop(1, #ffffff));
background-image: -webkit-linear-gradient(top, #ffffff, #ffffff);
background-image: -o-linear-gradient(top, #ffffff, #ffffff);
background-image: -ms-linear-gradient(top, #ffffff, #ffffff);
background-image: linear-gradient(top, #ffffff, #ffffff);”>
</di>
<di id=”drad” style=”position: absolute; top: 10%; left: 550px; width: 500px; height: 500px; border: 2px solid black; border-radius: 500px;
background-image: -moz-radial-gradient(center 45deg, circle closest-corner, #ffffff 10%, #ffffff 70%);
background-image: -webkit-radial-gradient(center center, circle closest-corner, #ffffff 10%, #ffffff 70%);
background-image: -ms-radial-gradient(center center, circle closest-corner, #ffffff 10%, #ffffff 70%);
background-image: -o-radial-gradient(center center, circle closest-corner, #ffffff 10%, #ffffff 70%);
“>

var brsr = “”;
if (navigator.userAgent.toLowerCase().indexOf(‘msie’) != -1 || navigator.userAgent.toLowerCase().indexOf(‘trident’) != -1 || navigator.userAgent.toLowerCase().indexOf(‘edge’) != -1) brsr = “ms”;

if ( navigator.userAgent.toLowerCase().indexOf(‘safari’) != -1 || navigator.userAgent.toLowerCase().indexOf(‘chrome’) != -1) {
brsr = “webkit”;
} else if ( navigator.userAgent.toLowerCase().indexOf(‘opera’) != -1) {
brsr = “o”;
} else if ( navigator.userAgent.toLowerCase().indexOf(‘firefox’) != -1) {
brsr = “moz”;
}

function gradGen() {
var direc = “top”;
var rad = false;
if (document.getElementById(“ch1”).checked) direc = “left”;

if (document.getElementById(“ch2”).checked) {
rad = true;
} else {
rad = false;
}

if (rad) {
if (brsr == “moz”) {
var grd = ‘-moz-radial-gradient(center 45deg, circle closest-corner,’ + document.getElementById(“color1”).value + ‘ ‘ + document.getElementById(“t2”).value + ‘,’ + document.getElementById(“color2”).value + ‘ ‘ + document.getElementById(“t3”).value + ‘)’;
document.getElementById(“drad”).style.backgroundImage = grd;
document.getElementById(“t1”).value = ‘style.backgroundImage = “‘ + document.getElementById(“drad”).style.backgroundImage + ‘”;’;
} else if (rad) {
grd = ‘ -‘ + brsr + ‘-radial-gradient(center center, circle closest-corner,’ + document.getElementById(“color1”).value + ‘ ‘ + document.getElementById(“t2”).value + ‘,’ + document.getElementById(“color2”).value + ‘ ‘ + document.getElementById(“t3”).value + ‘)’;
document.getElementById(“drad”).style.backgroundImage = grd;
document.getElementById(“t1”).value = ‘style.backgroundImage = “‘ + document.getElementById(“drad”).style.backgroundImage + ‘”;’;
}
}
if (! rad) {
grd = ‘ -‘ + brsr + ‘-linear-gradient(‘ + direc + ‘, ‘ + document.getElementById(“color1”).value + ‘,’ + document.getElementById(“color2”).value + ‘)’;
document.getElementById(“dlin”).style.backgroundImage = grd;
document.getElementById(“t1”).value = ‘style.backgroundImage = “‘ + document.getElementById(“dlin”).style.backgroundImage + ‘”;’;
}
var t1 = document.getElementById(“t1”);
t1.focus();
t1.select();
}

</body></html>


The gradient statements are browser specific, so a different one is needed for each browser. However, most differ only in a browser prefix.

Therefore, I created a variable brsr which was incorporated in general statements. A variable grd was created to hold the gradient string. For a linear gradient:
grd = ‘ -‘ + brsr + ‘-linear-gradient(‘ + direc + ‘, ‘ + document.getElementById(“color1”).value + ‘,’ + document.getElementById(“color2”).value + ‘)’;

direc is either top or left, depending on whether the gradient is vertical or horizontal.

A division was then loaded with the gradient and a text input given the value of grd and selected:
document.getElementById(“dlin”).style.backgroundImage = grd;
document.getElementById(“t1”).value = ‘style.backgroundImage = “‘ + document.getElementById(“dlin”).style.backgroundImage + ‘”;’;
}
var t1 = document.getElementById(“t1”);
t1.focus();
t1.select();

For radial gradients, Firefox differs:
if (brsr == “moz”) {
var grd = ‘-moz-radial-gradient(center 45deg, circle closest-corner,’ + document.getElementById(“color1”).value + ‘ ‘ + document.getElementById(“t2”).value + ‘,’ + document.getElementById(“color2”).value + ‘ ‘ + document.getElementById(“t3”).value + ‘)’;
} else if (rad) {
grd = ‘ -‘ + brsr + ‘-radial-gradient(center center, circle closest-corner,’ + document.getElementById(“color1”).value + ‘ ‘ + document.getElementById(“t2”).value + ‘,’ + document.getElementById(“color2”).value + ‘ ‘ + document.getElementById(“t3”).value + ‘)’;

rad is a boolean variable determined by the state of the second checkbox.

The following code determines the browser:
if (navigator.userAgent.toLowerCase().indexOf(‘msie’) != -1 || navigator.userAgent.toLowerCase().indexOf(‘trident’) != -1 || navigator.userAgent.toLowerCase().indexOf(‘edge’) != -1) brsr = “ms”;

if ( navigator.userAgent.toLowerCase().indexOf(‘safari’) != -1 || navigator.userAgent.toLowerCase().indexOf(‘chrome’) != -1) {
brsr = “webkit”;
} else if ( navigator.userAgent.toLowerCase().indexOf(‘opera’) != -1) {
brsr = “o”;
} else if ( navigator.userAgent.toLowerCase().indexOf(‘firefox’) != -1) {
brsr = “moz”;
}

Pre-Submission Form Verification with HTML


This post demonstrates some new HTML5 features.

It uses placeholders and makes all inputs required.

Additionally it has date, telephone, email, url and number inputs.

The placeholders in this case are titles for the input, but they can be any string. They are visible as gray but are not input values.

If an input with no value is moused over, a popup appears:

If the form is submitted before all required fields are filled, a popup appears at the first vacant field:

A popup appears at the first incorrectly filled field:

The form cannot be submitted until every required field is correctly filled.

This pre-submission verification will be exploited in a future post.

Here is the code:

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”&gt;
<html xmlns=”http://www.w3.org/1999/xhtml”&gt;
<head profile=”http://gmpg.org/xfn/11″&gt;
<title>HTML5 Validation</title>
<style>
body {margin-left:0;margin-right:0;font:normal normal normal 15px Arial; background: #666666}
a{ text-decoration: }:link { color: rgb(0, 0, 255) }:visited {color :rgb(100, 0,100) }:hover { }:active { }
form{position: absolute; width: 200px; left: 50%; margin-left: -100px;}
input{font:normal normal 700 20px Arial; border: 4px ridge}
</style>
</head>
<body>
<form action=”temp.html” method=”post” >
<input type=”text” id=”t1″ name=”t1″ value=”” placeholder=”text” required /><br />
<input type=”date” id=”date1″ name=”date1″ required /><br />
<input type=”tel” id=”tel1″ name=”tel1″ value=”” placeholder = “phone” required /><br />
<input type=”email” id=”e1″ name=”e1″ value=”” placeholder = “email” required/><br />
<input type=”url” id=”u1″ name=”u1″ value=”” placeholder = “url” required /><br />
<input type=”number” id=”n1″ name=”n1″ value=”” placeholder = “number” required /><br />
<button type=”submit” id=”submit”style = “font:normal normal 700 20px Arial” name=”submit” value=”submit” >Submit</button>
</form>
</body></html>