View Full Version : Advice on Script

07-06-2011, 03:38 PM
New to javascript...I'm looking to see if:

1. Is there is a better way to do this? Is this a good way or is there something else I should be doing?

2. I have 8 different fields I have to pair up and check all in sequential order. Example, checking earnings_work1 to earnings_work_occur1, checking earnings_work2 to earnings_work_occur2, etc. Do I need to repeat the script below 8 times or where should I be heading?

function validateForm()

if (form1.earnings_work1.value.length != 0)
if (form1.earnings_work_occur1.value.length == 0) {
alert("You must enter an occurence.");
return (false);

// End -->

07-07-2011, 08:48 AM
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<script type="text/javascript">

function validateForm(){
for (var z0=1;z0<5;z0++){
if (form1['earnings_work'+z0].value.length != 0&&form1['earnings_work_occur'+z0].value.length == 0){
alert("You must enter an occurence.");
return (false);


<form name="form1" >
<input name="earnings_work1" /><input name="earnings_work_occur1" />
<br />
<input name="earnings_work2" /><input name="earnings_work_occur2" />
<br />
<input name="earnings_work3" /><input name="earnings_work_occur3" />
<br />
<input name="earnings_work4" /><input name="earnings_work_occur4" />
<br />
<input type="button" name="" value="Check" onmouseup="validateForm()"/>


07-07-2011, 09:24 AM
tbh I'd just use jQuery with the validation plugin/module.