# Javascript Issue in a contact form

**URL:** <https://forum.kirupa.com/t/javascript-issue-in-a-contact-form/300798>\
**Category:** web dev\
**Created:** [November 27, 2009, 7:56pm UTC](https://forum.kirupa.com/t/javascript-issue-in-a-contact-form/300798 "2009-11-27T19:56:35Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![thevibe](https://avatars.discourse-cdn.com/v4/letter/t/d2c977/32.png) [@thevibe](https://forum.kirupa.com/u/thevibe)\
**Post date:** [November 27, 2009, 7:56pm UTC](https://forum.kirupa.com/t/javascript-issue-in-a-contact-form/300798/1 "2009-11-27T19:56:35Z")

</div>

Hi guys,

I am working on contact forms for a friend. These forms have a Javascript code to tell the user when he didn’t filled all the required fields. I tried it on 1 form and it’s working well. But I have an issue with the 2 other forms. I checked it throughly and found what was causing the problem but I don’t know the solution. In fact the problem is that, in the 2 forms, some fields share the same name. And Javascript stop working when there’s more than 1 field with the same name. I have an important constraint. I need to use specific names for the field cause it’s already in place in a server, and we cannot change them.

Can you think of any other solution ?

Here’s an example of 1 form. In this example I have 3 fields called txtMessage.

\<head\>  
\<script type=“text/javascript” language=“JavaScript”\>

var FormName = “form2”;  
var RequiredFields = “txtContactFirstName,txtContactLastName,txtContact Email,txtMessage”;

function ValidateRequiredFields()  
{  
var FieldList = RequiredFields.split(",")  
var BadList = new Array();  
for(var i = 0; i \< FieldList.length; i++) {  
var s = eval(‘document.’ + FormName + ‘.’ + FieldList\* + ‘.value’);  
s = StripSpacesFromEnds(s);  
if(s.length \< 1) { BadList.push(FieldList\*); }  
}  
if(BadList.length \< 1) { return true; }  
var ess = new String();  
if(BadList.length \> 1) { ess = ‘s’; }  
var message = new String(‘You have not completed some or all of the required fields.’);  
alert(message);  
return false;  
}

function StripSpacesFromEnds(s)  
{  
while((s.indexOf(’ ‘,0) == 0) && (s.length\> 1)) {  
s = s.substring(1,s.length);  
}  
while((s.lastIndexOf(’ ‘) == (s.length - 1)) && (s.length\> 1)) {  
s = s.substring(0,(s.length - 1));  
}  
if((s.indexOf(’ ',0) == 0) && (s.length == 1)) { s = ‘’; }  
return s;  
}  
// --\>  
\</script\>

And here’s an example of how it’s written in html;

\<input type=“hidden” name=“txtMessage” value=“REFERRAL TO:”\>  
\<tr\>  
\<td width=“148” align=right valign=top style=“font-family: verdana, helvetica; font-size: 8pt; padding: 5px; color: #ffffff;”\>\* FULL NAME: \</td\>  
\<td width=“298” style=“padding: 5px 5px 5px 0px;”\>\<input type=“text” name=“txtMessage” size=34\>\</td\>\</tr\>  
\<tr\>  
\<td align=right valign=top style=“font-family: verdana, helvetica; font-size: 8pt; padding: 5px; color: #ffffff;”\>\* PHONE NUMBER: \</td\>\<td style=“padding: 5px 5px 5px 0px;”\>\<input type=“text” name=“txtMessage” size=34 \>\</td\>\</tr\>  
\<tr\>  
\<td align=right valign=top style=“font-family: verdana, helvetica; font-size: 8pt; padding: 5px; color: #ffffff;”\>\* EMAIL ADDRESS: \</td\>\<td style=“padding: 5px 5px 5px 0px;”\>\<input type=“text” name=“txtMessage” size=34\>\</td\>\</tr\>
