<form onsubmit="return ceaserShift(event)" name="myForm" method="post">
<h4>
An Example to encrypt your input via Ceaser Cipher
</h4>
<label>Input</label>
<input type="text" placeholder="Enter your text" name="inputTxt" />
<br>
<label>OutPut</label>
<input type="text" placeholder="Enter your text" readonly name="outputTxt" />
<br>
<input type="submit" value="Encrypt" />
</form>
<br>
JavaScript
function ceaserShift(e) {
e.preventDefault();
let str = document.forms["myForm"]["inputTxt"].value;
let amount = 90;
if (amount < 0) {
return ceaserShift(str, amount + 26)
}
// we will store the output here
let output = '';
for (let i = 0; i < str.length; i++) {
// holding the value
let ch = str[i];
if (ch.match(/[a-z]/i)) {
// getting the code for that letter
let code = str.charCodeAt(i)
// checking for uppercase letters
if ((code >= 65) && (code <= 90)) {
ch = String.fromCharCode(((code - 65 + amount) % 26) + 65);
}
// checking for lowercase letters
else if ((code >= 97) && (code <= 122)) {
ch = String.fromCharCode(((code - 97 + amount) % 26) + 97);
}
}
// appending the output
output += ch;
}
document.forms["myForm"]["outputTxt"].value = output
return output;
}
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.