PDA

View Full Version : JavaScript Help


Bobba Buoy
12-23-2003, 07:18 AM
I am re-writing my field validations to be more compact. The following is a function that I use validate fields. How can I use variables or loops to clean it up? I am pretty well versed (?) in asp but not so much in javascript. Any help would be much appreciated!

Thanks!!


function CheckFields() {
if (document.eventreg1.firstname.value == '' ||
document.eventreg1.lastname.value == '' ||
document.eventreg1.address.value == '' ||
document.eventreg1.city.value == '' ||
document.eventreg1.state.value == '' ||
document.eventreg1.zip.value == '' ||
document.eventreg1.phone1.value == '' ||
document.eventreg1.phone2.value == '' ||
document.eventreg1.phone3.value == '' ||
document.eventreg1.email.value == '' ||
document.eventreg1.userid.value == '' ||
document.eventreg1.password1.value == '' ||
document.eventreg1.password2.value == '' )
{
alert('Please fill in all required fields');
return false
}
else
if (document.eventreg1.password1.value != document.eventreg1.password2.value)
{
alert('Passwords do not match');
return false
}
else
if (isNaN(document.eventreg1.zip.value))
{
alert('The zip code field can not contain non-numeric values');
return false
}
else
if (isNaN(document.eventreg1.phone1.value))
{
alert('The age field can not contain non-numeric values');
return false
}
else
if (isNaN(document.eventreg1.phone2.value))
{
alert('The age field can not contain non-numeric values');
return false
}
else
if (isNaN(document.eventreg1.phone3.value))
{
alert('The age field can not contain non-numeric values');
return false
}
else
if (document.eventreg1.zip.value.length != 5)
{
alert('Zip codes must be contain 5 characters.');
return false
}
else
if (document.eventreg1.state.value.length != 2)
{
alert('The state field must contain 2 characters.');
return false
}
else
if (document.eventreg1.phone1.value.length != 3)
{
alert('The area code field must contain 3 characters.');
return false
}
else
if (document.eventreg1.phone2.value.length != 3)
{
alert('The second phone field must contain 3 characters.');
return false
}
else
if (document.eventreg1.phone3.value.length != 4)
{
alert('The third phone field must contain 4 characters.');
return false
}
else
if (document.eventreg1.userid.value.length < 6)
{
alert('The userid field must be at least 6 characters long.');
return false
}
else
if (document.eventreg1.password1.value.length < 6)
{
alert('Your password must be at least 6 characters long.');
return false
}
else
return true

Willy Duitt
12-23-2003, 03:18 PM
Something like this should work.
Note: This is untested since you failed to include
your form codes and I am too lazy to replicate your form
in order to test. :D

<script type="text/javascript">
function CheckFields(){
theForm = document.eventreg1;
for(i=0; i<theForm.elements.length; i++){
if (theForm.elements[i].value == '' ){
alert('Please fill in all required fields');
return false;
}
if (isNaN(theForm.elements[i].value)){
alert('The '+theForm.elements[i].name+' field can not contain non-numeric values');
return false;
}
else{
return true;
}
}
}
</script>

.....Willy

Bobba Buoy
12-23-2003, 03:24 PM
No problem on the testing issue. I just need a kind of a 'big picture' approach to condensing this validation. this should work very well. Thanks!

Willy Duitt
12-23-2003, 03:29 PM
This may help your 'Big Picture'

Looping thru form elements array (http://www.jennifermadden.com/162/examples/loopForms.html).

Happy Holidays;
.....Willy

Bobba Buoy
12-24-2003, 12:08 PM
I am implementing the script and I have a couple of questions:
1) how does the function distinguish between fields that are required and fields that can be left blanK/
2) How does the NaN part of the function distinguish between numeric and non-numeric fields?

Thanks!

Willy Duitt
12-29-2003, 07:17 AM
Originally posted by Bobba Buoy
I am implementing the script and I have a couple of questions:
1) how does the function distinguish between fields that
are required and fields that can be left blanK/
2) How does the NaN part of the function distinguish
between numeric and non-numeric fields?

Thanks!

Just seen this looking for something else. :eek:
Hope your Holiday was a joyous as mine was. :D

1) It doesn't! Write another conditional statement
using the element array[number] to exclude those fields.

eg:
<script type="text/javascript">
function CheckFields(){
theForm = document.eventreg1;
for(i=0; i<theForm.elements.length; i++){
if (theForm.elements[1].value=='' || theForm.elements[6].value==''){
return true;
}
else if (theForm.elements[i].value=='' ){
alert('Please fill in all required fields');
return false;
}
if (isNaN(theForm.elements[i].value)){
alert('The '+theForm.elements[i].name+' field can not contain non-numeric values');
return false;
}
else{
return true;
}
}
}
</script>

2) That was taken from your example. You must have
a is number validation function which you did not include.

.....Willy

Willy Duitt
12-29-2003, 07:34 AM
FWIW: You could also just use the value of i

eg:

if (i==1 || i==6 && value==''){
return true;
}

Or if the excluded fields are grouped together:

if (i>6 || i<10 && value==''){
return true;
}

.....Willy