PDA

View Full Version : Form Validation Functions


whammy
04-28-2011, 04:58 AM
I posted these literally years ago (2003) to help people out, lots of JavaScript functions that I used to use when I was doing web development for a living, and they covered most requests I had to deal with at the time.

If you know how to use JavaScript functions, these are definitely helpful for client-side form validation and the regular expressions can be used in other programming languages as well. Some are from this forum, some I wrote, and some are a combination (or an improvement):


/********************************************************************************
JavaScript Functions
by Rob Davis 2003
*********************************************************************************/

// Hide email addresses from spiders
// Sample usage:
// maskEmail("email","domain.com","Text");
function maskEmail(u,d,t)
{
var e = (u + '@' + d).removeSpaces();
document.write('<a href="mailto:' + e + '">');
if (t) document.write(t); else document.write(e);
document.write('</a>');
}

// Remove spaces
String.prototype.removeSpaces = function()
{
return this.replace(/\s+/g,'');
}

// Compress spaces
String.prototype.compress = function()
{
return this.replace(/\s+/g,' ').trim();
}

// Trim
String.prototype.trim = function()
{
return this.replace(/^\s+/,'').replace(/\s+$/,'');
}

// Validate email
String.prototype.isEmail = function()
{
return /^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/.test(this);
}

// Convert to alpha
String.prototype.toAlpha = function()
{
return this.replace(/[^a-z]/gi,'');
}

// Convert to alphanumeric
String.prototype.toAlphaNumeric = function()
{
return this.replace(/[^a-z0-9]/gi,'');
}

// Convert to numeric
String.prototype.toNumeric = function()
{
return this.replace(/\D/g,'');
}

// Convert to word characters
String.prototype.toWordChars = function()
{
return this.replace(/\W/g,'');
}

// Convert to allowed characters
String.prototype.toAllowedChars = function()
{
return this.replace(/[^a-zA-Z0-9 &#-.,]/g,'');
}

// Only allow numeric keypresses
function isNumericKey(e)
{
var k = document.all ? e.keyCode : e.which;
return ((k > 47 && k < 58) || k == 8 || k == 0 || k == 13);
}

// Create global variables from querystring values
function obj_queryString()
{
var qs = location.search.substring(location.search.indexOf('?') + 1,location.search.length);
var queries = qs.split(/\&/);
for (i = 0; i < queries.length; i++)
{
var query = queries[i].split(/\=/);
this[query[0]] = unescape(query[1]);
}
}
queryString = new obj_queryString();

// Generic checkAll function
function checkAll(formObj,bool,fieldName)
{
var len = formObj.elements.length;
for (var i = 0; i < len; i ++)
{
if (formObj.elements[i].type == "checkbox" &&
formObj.elements[i].name == fieldName)
{
formObj.elements[i].checked = bool;
}
}
}

function openWindow(URL,width,height,t,l,s,r)
{
var w = screen.width;
var h = screen.height;
var left = (w - width) / 2;
var top = (h - height) / 2;
var args = "toolbar=" + t + ",location=" + l + ",scrollbars=" +
s + ",resizable=" + r + ",width=" + width + ",height=" +
height + ",left=" + left + ",top=" + top;
seWin = window.open(URL,"seWin",args);
seWin.focus();
}

function formatPhone(phone)
{
var regex = /^(?:1?[ .-]?)\(?([1-9]\d{2})\)?[ .-]?\s?(\d{3})[ .-]?(\d{4})$/
if (regex.test(phone))
{
return phone.replace(regex,"($1) $2-$3");
}
else
{
return "";
}
}

function isDate(mm,dd,yyyy)
{
var d = new Date(mm + "/" + dd + "/" + yyyy);
return d.getMonth() + 1 == mm && d.getDate() == dd && d.getFullYear() == yyyy;
// A JavaScript trick I discovered
}

function denySpaces(e)
{
var k = document.all ? e.keyCode : e.which;
return k != 32;
}

function uncheckAll(formObj,ctrlField,fieldName)
{
var len = formObj.elements.length;
if (ctrlField.checked)
{
for (var i = 0; i < len; i ++)
{
if (formObj.elements[i].type == "checkbox" &&
formObj.elements[i].name.split("_")[0] == fieldName &&
formObj.elements[i].name != ctrlField.name)
{
formObj.elements[i].checked = false;
}
}
}
}


I haven't coded much since around 2k3 so if you see anything out of date (i.e. the stuff dealing with internet explorer, etc.) please let me know.