Home »
tech forum
//digital things Tech Forum
The solutions below are free to copy and use, no strings attached. You can rename the functions if you want but you might want to keep the Copyright Notice so credit will be given where credit is due. If you don't, no big deal, it's your karma after all...
If you used and liked (or disliked) any of our solutions, your feedback is always welcome.
Solutions in this page:
- JavaScript: Validate credit cards
- JavaScript: Real rounding
- JavaScript: Check for empty field
- JavaScript: Simple AJAX library
- ASP/ASP.NET: Code generation trick
1. JavaScript: Validate credit cards
This routine cannot tell you whether customers are over their spending limit but at least it allows you to detect phony numbers before the bank does. Returns true if number passes LUHN test, false otherwise.
//
// Check LUHN - Copyright © //digital things - www.digithings.com
//
function DTCValidateLUHN(ccNum)
{
var ccLen = ccNum.length;
var oddoeven = ccLen & 1;
var sum = 0;
for (var n = 0; n < ccLen; n++)
{
var digit = parseInt(ccNum.charAt(n));
if (!((n & 1) ^ oddoeven))
{
digit *= 2;
digit = digit > 9 ? digit-9 : digit;
}
sum += digit;
}
return sum % 10 ? false : true;
}
2. JavaScript: Real rounding
If you have ever worked with JavaScript, you have probably noticed that the Math.round() function only rounds numbers into integers, equivalent to a call to VB/ASP/SQL Round(n, 0).
We needed a reliable way of rounding floats and we created the DTCRound() function to this effect.
The function uses powers of 10 to provide a functionality similar to the VB/ASP/SQL Round() function.
Warning: if using in JSP page, make sure to fit the "return(...)" statement on the same line, no line breaks!
//
// DTCRound() - Copyright © //digital things - www.digithings.com
// Accurately rounds decimals.
//
function DTCRound(value, decimals)
{
return(
parseFloat(value) != "NaN" && parseInt(decimals) != "NaN"
? (Math.round(value * Math.pow(10, decimals))) / Math.pow(10, decimals)
: "NaN"
);
}
3. JavaScript: Check for empty field
When you do client-side validation, you sometimes want to make sure that the user is not trying to fool you by entering "spaces" to make the application believe a required field is not empty.
The function below handles this case. You just pass it a "form.fieldname" as parameter and it does the rest, returning true if the field is empty, false otherwise.
//
// DTCFieldIsEmpty() - Copyright © //digital things - www.digithings.com
// Returns true if the passed field is empty (null or spaces), false otherwise.
//
function DTCFieldIsEmpty(field)
{
var i = 0;
var n = 0;
// Field is null.
if (field.value.length == 0)
return true;
// Scan string to make sure it is not just white space (number of spaces = length).
for (i = 0; i < field.value.length; i++)
{
if (field.value.charAt(i) == " ")
n++;
}
if (n && i == n)
return true;
return false;
}
4. JavaScript: Simple AJAX library
Want to implement a quick but solid AJAX solution? This simple library allows to to load any dynamic content with a single line of JS code!
What you pull into the receiving DIV in your document is up to you, from plain text, image or video up to the results of a complex process outputting results in any valid HTML format.
How to:
1. Declare a reference to the JS library into your HTML document (see library code below):
<script language="javascript" src="/js/DTAjaxLib.js">
</script>
2. Make sure you create a DIV in your document where the AJAX content will end up.
<div id="myDIV"></div>
3. Call the DTLoadAjaxContent() function to populate the DIV. You may call the function on field change, button click, mouse click, etc...
<script language="javascript">
DTCLoadAjaxContent("myDIV", "http://www.mydomain.com/mypage.htm");
</script>
Save code below as "DTAjaxLib.js", preferably in a "/js" folder on your site to keep it all organized.
//
// Simple AJAX interface library - Copyright © //digital things - www.digithings.com
//
// Ajax global variables.
var _ajaxDIVTarget = null;
var xmlHttp = null;
// AJAX interface.
function DTCGetXmlHttpObject(handler)
{
var oXmlHttp = null;
// ActiveX browsers (IE).
if (navigator.userAgent.indexOf("MSIE") >=0)
{
var objName = "Msxml2.XMLHTTP";
if (navigator.appVersion.indexOf("MSIE 5.5") >= 0)
objName = "Microsoft.XMLHTTP";
try
{
oXmlHttp = new ActiveXObject(objName);
oXmlHttp.onreadystatechange = handler;
return oXmlHttp;
}
catch(e)
{
alert("Please adjust your browser settings to allow ActiveX objects.");
return;
}
}
// Other browsers.
if (navigator.userAgent.indexOf("Mozilla") >= 0)
{
oXmlHttp = new XMLHttpRequest();
oXmlHttp.onload = handler;
oXmlHttp.onerror = handler;
return oXmlHttp;
}
}
// AJAX event handler.
function DTCStateChanged()
{
if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete")
document.getElementById(_ajaxDIVTarget).innerHTML = xmlHttp.responseText;
}
// AJAX content loader.
function DTCLoadAjaxContent(div, url)
{
// Set target DIV.
_ajaxDIVTarget = div;
// Load AJAX content.
xmlHttp = DTCGetXmlHttpObject(DTCStateChanged);
xmlHttp.open("GET", url, true);
xmlHttp.send(null);
}
5. ASP/ASP.NET Trick
Highly flexible, Active Server Pages and ASP.NET and their VB-like language are a neat way to provide dynamic content to web sites. Since ASP code is processed at the server level, it also allows generation of code (HTML, XML or JavaScript, etc...) that will be executed by the browser or even generation of HTML files using the File Scripting Object. Talk about flexible!
Here is a trick that allows your ASP page to generate ASP code (for instance if you generate template pages from an ASP application). This requires a trick to prevent your ASP page to interpret the generated ASP literally.
Example:
We want to output a call to an ASP sub-routine call while using ASP to write an ASP file:
The statement below generates a syntax error because ASP interprets the tags literally:
fs.WriteLine "<%rc=myRoutine(prm1, prm2, ...)%>" <--- Syntax error!
How-to:
The ASP interpreter needs to be fooled by supplying the needed tags in a way it does not recognize and therefore will ignore:
fs.WriteLine "<" & Chr(37) & "rc=myRoutine(prm1, prm2, ...)" & Chr(37) & ">"
When we substitute the % character with its equivalent Chr(37), the interpreter leaves us alone and allows the function to generate the desired ASP/ASP.NET code in the page.
The same trick works when generating file inclusion statements:
inclBegin = "<!-- #"
inclEnd = " -->"
fs.WriteLine inclBegin & "include virtual=/myinclude.inc" & inclEnd
DISCLAIMER: The solutions above, while tested and working are provided "as is" with no guarantee whatsoever. We do not provide free support nor can be we held responsible for any damage, direct, consequential, incidental or otherwise arising from the use of solutions or derivatives. By downloading and/or using the solutions above, you agree to these terms.