kbjradmin
New Member
- Messages
- 512
- Reaction score
- 2
- Points
- 0
i am working on a site for my web developement class and am having some trouble with validating the contents of a form. the page i'm having trouble on is at http://cs.clark.edu/~jbrum4030/ctec122/final/order.htm
(before anyone says anything, no, none of this works in IE, but the instructor doesn't care about IE, so for the time being, neither do i. and if you're using IE, don't.)
here is the validation script i have so far...
everything works except the address field, the city/state/zip-code field, and the phone number field.
please help!
edit:
i got phone to work, but the other two still don't.
(before anyone says anything, no, none of this works in IE, but the instructor doesn't care about IE, so for the time being, neither do i. and if you're using IE, don't.)
here is the validation script i have so far...
Code:
function checkForm(type,id)
{
if ( id == 'all')
{
requiredFields = document.getElementsByClassName('required-done');
for ( var i in requiredFields )
{
requiredFields[i].setAttribute('class', 'required');
}
}
switch(type)
{
case 'text':
if ( textField(id) )
{
document.getElementById(id + '-required').setAttribute('class', 'required-done');
}
else
{
document.getElementById(id + '-required').setAttribute('class', 'required');
}
break;
case 'num':
if ( numField(id) )
{
document.getElementById(id + '-required').setAttribute('class', 'required-done');
}
else
{
document.getElementById(id + '-required').setAttribute('class', 'required');
}
break;
case 'digit':
if ( digitField(id) )
{
document.getElementById(id + '-required').setAttribute('class', 'required-done');
}
else
{
document.getElementById(id + '-required').setAttribute('class', 'required');
}
break;
case 'address':
if ( addressField(id) )
{
document.getElementById(id + '-required').setAttribute('class', 'required-done');
}
else
{
document.getElementById(id + '-required').setAttribute('class', 'required');
}
break;
case 'citySZ':
if ( textField(id[0]) && document.getElementById(id[1]) && numField(id[2]) )
{
document.getElementById('citySZ-required').setAttribute('class', 'required-done');
}
else
{
document.getElementById('citySZ-required').setAttribute('class', 'required');
}
break;
case 'phone':
if ( digitField(id[0],3) && digitField(id[1],3) && digitField(id[2],4) )
{
document.getElementById('phone-required').setAttribute('class', 'required-done');
}
else
{
document.getElementById('phone-required').setAttribute('class', 'required');
}
break;
case 'email':
if ( emailField(id) )
{
document.getElementById(id + '-required').setAttribute('class', 'required-done');
}
else
{
document.getElementById(id + '-required').setAttribute('class', 'required');
}
break;
default:
// code here
break;
}
}
function textField(id,value)
{
if ( value )
{
var value = value;
}
else
{
if ( id instanceof Array )
{
for ( i in id )
{
var value = document.getElementById(id[i]).value;
if ( value != null && value != '' )
{
return true;
}
else
{
return false;
}
}
}
else
{
var value = document.getElementById(id).value;
if ( value != null && value != '' )
{
return true;
}
else
{
return false;
}
}
}
}
function numField(id,value)
{
if ( value )
{
var value = value;
}
else
{
var value = document.getElementById(id).value;
}
if ( isNaN( value ) )
{
return false;
}
else if ( value == null || value == '' )
{
return false;
}
else
{
return true;
}
}
function digitField(id,len,value)
{
if ( value )
{
var value = value;
}
else
{
var value = document.getElementById(id).value;
}
len = len || 'any';
if ( value.match(/^\d+$/) )
{
if ( value.length == len || len == 'any' )
{
return true;
}
}
return false;
}
function emailField(id,value)
{
if ( value )
{
var str = value;
}
else
{
var str = document.getElementById(id).value;
}
var at="@";
var dot=".";
var lat=str.indexOf(at);
var lstr=str.length;
var ldot=str.indexOf(dot);
if (str.indexOf(at)==-1)
{
return false;
}
if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr)
{
return false;
}
if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr)
{
return false;
}
if (str.indexOf(at,(lat+1))!=-1)
{
return false;
}
if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot)
{
return false;
}
if (str.indexOf(dot,(lat+2))==-1)
{
return false;
}
if (str.indexOf(" ")!=-1)
{
return false;
}
return true;
}
function addressField(id,value)
{
if ( value )
{
var address = value;
}
else
{
var address = document.getElementById(id).value;
}
var addressArray = address.split(' ');
if ( address && addressArray[0] && addressArray[1] && addressArray[2] && addressArray[3] )
{
if (
digitField(null,'any',addressArray[0]) &&
addressArray[1] in new Array( 'n','s','e','w','ne','nw','se','sw' ) &&
textField(null,addressArray[2]) &&
textField(null,addressArray[3])
)
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
function checkNumSuffix(value)
{
if ( ! value ) { return false; }
var len = value.length;
var suffix = value[len-2] + value[len-1];
if ( digitField(null,null,value.substring(0,len-2)) )
{
if ( suffix in new Array( 'st', 'nd', 'rd', 'th' ) )
{
return suffix;
}
else
{
return false;
}
}
else
{
return false;
}
}
everything works except the address field, the city/state/zip-code field, and the phone number field.
please help!
edit:
i got phone to work, but the other two still don't.
Last edited: