Forums
This topic is locked
Form Validation Problem...Pls Help.
Posted 11 Jul 2003 16:46:43
1
has voted
11 Jul 2003 16:46:43 Katherine Williams posted:
I have an Update form that needs validation. I tried to use UltraDev 4's built-in validation, but it can't validate list/menu's, radiobuttons, or checkboxes. So I went to JavaScriptSource.com, and got some code. It works great at validating everything but radiobuttons & checkboxes.I'm hoping that someone can view the code below, and let me know how I can validate radiobuttons & checkboxes. Or if someone can let me know how to validate list/menu's, radiobuttons, or checkboxes using UltraDev 4's built-in validation, that would be great!!! Thanks for any help in advance.
<b>1) validation.js (JS include):</b> This page does the actual work.
// Generic Form Validation
// Jacob Hage ( )
var checkObjects = new Array();
var errors = "";
var returnVal = false;
var language = new Array();
language["header"] = "The following error(s) occured:"
language["start"] = "->";
language["field"] = " Field ";
language["require"] = " is required";
language["min"] = " and must consist of at least ";
language["max"] = " and must not contain more than ";
language["minmax"] = " and no more than ";
language["chars"] = " characters";
language["num"] = " and must contain a number";
language["email"] = " must contain a valid e-mail address";
// -----------------------------------------------------------------------------
// define - Call this function in the beginning of the page. I.e. onLoad.
// n = name of the input field (Required)
// type= string, num, email (Required)
// min = the value must have at least [min] characters (Optional)
// max = the value must have maximum [max] characters (Optional)
// d = (Optional)
// -----------------------------------------------------------------------------
function define(n, type, HTMLname, min, max, d) {
var p;
var i;
var x;
if (!d) d = document;
if ((p=n.indexOf("?")>0&&parent.frames.length) {
d = parent.frames[n.substring(p+1)].document;
n = n.substring(0,p);
}
if (!(x = d[n]) && d.all) x = d.all[n];
for (i = 0; !x && i < d.forms.length; i++) {
x = d.forms[i][n];
}
for (i = 0; !x && d.layers && i < d.layers.length; i++) {
x = define(n, type, HTMLname, min, max, d.layers[i].document);
return x;
}
eval("V_"+n+" = new formResult(x, type, HTMLname, min, max);"
checkObjects[eval(checkObjects.length)] = eval("V_"+n);
}
function formResult(form, type, HTMLname, min, max) {
this.form = form;
this.type = type;
this.HTMLname = HTMLname;
this.min = min;
this.max = max;
}
function validate() {
if (checkObjects.length > 0) {
errorObject = "";
for (i = 0; i < checkObjects.length; i++) {
validateObject = new Object();
validateObject.form = checkObjects[i].form;
validateObject.HTMLname = checkObjects[i].HTMLname;
validateObject.val = checkObjects[i].form.value;
validateObject.len = checkObjects[i].form.value.length;
validateObject.min = checkObjects[i].min;
validateObject.max = checkObjects[i].max;
validateObject.type = checkObjects[i].type;
if (validateObject.type == "num" || validateObject.type == "string" {
if ((validateObject.type == "num" && validateObject.len <= 0) || (validateObject.type == "num" && isNaN(validateObject.val))) { errors += language['start'] + language['field'] + validateObject.HTMLname + language['require'] + language['num'] + "\n";
} else if (validateObject.min && validateObject.max && (validateObject.len < validateObject.min || validateObject.len > validateObject.max)) { errors += language['start'] + language['field'] + validateObject.HTMLname + language['require'] + language['min'] + validateObject.min + language['minmax'] + validateObject.max+language['chars'] + "\n";
} else if (validateObject.min && !validateObject.max && (validateObject.len < validateObject.min)) { errors += language['start'] + language['field'] + validateObject.HTMLname + language['require'] + language['min'] + validateObject.min + language['chars'] + "\n";
} else if (validateObject.max && !validateObject.min &&(validateObject.len > validateObject.max)) { errors += language['start'] + language['field'] + validateObject.HTMLname + language['require'] + language['max'] + validateObject.max + language['chars'] + "\n";
} else if (!validateObject.min && !validateObject.max && validateObject.len <= 0) { errors += language['start'] + language['field'] + validateObject.HTMLname + language['require'] + "\n";
}
} else if(validateObject.type == "email" {
// Checking existense of "@" and ".".
// Length of must >= 5 and the "." must
// not directly precede or follow the "@"
if ((validateObject.val.indexOf("@" == -1) || (validateObject.val.charAt(0) == "." || (validateObject.val.charAt(0) == "@" || (validateObject.len < 6) || (validateObject.val.indexOf("." == -1) || (validateObject.val.charAt(validateObject.val.indexOf("@"+1) == "." || (validateObject.val.charAt(validateObject.val.indexOf("@"-1) == ".") { errors += language['start'] + language['field'] + validateObject.HTMLname + language['email'] + "\n"; }
}
}
}
if (errors) {
alert(language["header"].concat("\n" + errors));
errors = "";
returnVal = false;
} else {
returnVal = true;
}
}
<b>2) app1.asp (Update page script):</b> This page specifies which form elements to validate, and how to do so.
<SCRIPT LANGUAGE="JavaScript">
<!-- Begin
function init() {
define('Last_Name', 'string', 'Last Name', 1, 30);
define('Middle_Name', 'string', 'Middle Name', null, 15);
define('First_Name', 'string', 'First Name', 1, 30);
define('Hm_Phone', 'string', 'Home Phone #', 1, 14);
define('Current_Address', 'string', 'Current Street Address', 1, 40);
define('Current_City', 'string', 'Current City', 1, 30);
define('Current_State', 'string', 'Current State', 2, 5);
define('Current_Other', 'string', 'Current Other', null, 30);
define('Current_Zip', 'string', 'Current Zip', 1, 5);
define('Current_Years', 'string', 'Current # of Years', 1, 3);
define('Previous_Address', 'string', 'Previous Street Address', null, 40);
define('Previous_City', 'string', 'Previous City', null, 30);
define('Previous_State', 'string', 'Previous State', null, 5);
define('Previous_Other', 'string', 'Previous Other', null, 30);
define('Previous_Zip', 'string', 'Previous Zip', null, 5);
define('Previous_Years', 'string', 'Previous # of Years', null, 3);
}
// End -->
</script>