[Javascript] Why won't this work????
Cutter (JavaScript)
javascript at theblades-family.com
Wed Jan 29 13:07:19 CST 2003
I've created a form validation script, but for some reason it keeps
choking after it does the 8th field the second time around. Anyone???
var fieldnames = new Array();
var Item = new Array();
fieldnames[0] = (Item =['name','text','Please supply your Name',0,0]);
fieldnames[1] = (Item = ['title','text','Please supply your Title',0,0]);
fieldnames[2] = (Item = ['company','text','Please supply your Company
name',0,0]);
fieldnames[3] = (Item = ['address','text','Please supply your
Address',0,0]);
fieldnames[4] = (Item = ['city','text','Please supply your City',0,0]);
fieldnames[5] = (Item = ['country','select','Please supply your
Country',0,0]);
fieldnames[6] = (Item = ['state_province','select','Please supply your
State or province',0,0]);
fieldnames[7] = (Item = ['zip_or_country_code','text','Please supply
your Zip or Country Code',0,0]);
fieldnames[8] = (Item = ['phone','text','Please supply your Phone
number',1,0]);
fieldnames[9] = (Item = ['fax','text','Please supply your Fax number',1,0]);
fieldnames[10] = (Item = ['email','text','You must enter a valid Email
address',1,1]);
function isEmpty(form, field)
{
fn = getField(field);
tp = getType(field);
result = false;
if(tp == 'text')
{
strValue = "form." + fn + ".value";
}
if(tp == 'select')
{
strValue = "form." + fn + ".options[form." + fn +
".selectedIndex].value";
}
if ((eval(strValue) == null) || (eval(strValue) == ''))
result = true;
return result;
}
function getField(field)
{
Item = field;
return Item[0];
}
function getType(field)
{
Item = field;
return Item[1];
}
function getError(field)
{
Item = field;
return Item[2];
}
function blValChar(field)
{
Item = field;
return Item[3];
}
function blValMail(field)
{
Item = field;
return Item[4];
}
function showError(field, form)
{
alert(getError(field));
a = "form." + getField(field);
eval(a).focus();
if(getType(field) == 'text')
eval(a).select();
}
function charCheck(field, form)
{
result = true;
var arrBadChars = new
Array("~","`","!","@","#","$","%","%","^","&","*","(",")","_","=",":",";","'","<",">",".","?","/");
strField = "form." + getField(field);
a = eval(strField).value;
for (i = 0; i < arrBadChars.length; i++)
{
if (a.indexOf(arrBadChars[i]) != '-1')
{
alert('Please do not enter any illegal characters');
eval(strField).focus();
eval(strField).select();
result = false;
return result;
}
}
return result;
}
function chkMail(field, form)
{
re= /^\w+([\.-]?\w)*@\w+([\.]?\w+)*(\.\w{2,4})+$/; // RegEx for
testing email
result = true;
strField = "form." + getField(field);
strValue = eval(strField).value;
if(!re.test(strValue))
result = false;
return result;
}
function valForm(form)
{
for( i = 0; i < fieldnames.length; i++ )
{
alert("Field " + i + " " + getField(fieldnames[i]));
if(isEmpty(form,fieldnames[i]))
{
showError(fieldnames[i],form);
return false;
}
if(blValChar(fieldnames[i]))
{
if(!charCheck(fieldnames[i], form))
return false;
}
if(blValMail(fieldnames[i]))
{
if(!chkMail(fieldnames[i]))
return false;
}
}
return true;
}
More information about the Javascript
mailing list