/    Sign up×
Community /Pin to ProfileBookmark

Form validation

Hi there:

I have this function which validates chosend fields from the form and gives the alert poop up box on submission listing which all fields were not filled.

But for some reason…even after leaving al the fields blank..i am not getting any pop up box…here is my function:

[code]
function validateForm()
{
var count=0;
var msg=””;
var ele = new Array();
ele[0]=”firstname”;
ele[1]=”lastname”;
ele[2]=”address1″;
ele[3]=”city”;
ele[4]=”state”;
ele[5]=”zip”;
ele[6]=”email”;
ele[7]=”dayphone”;
ele[8]=”programofinterest”;
ele[9]=”campus”;
ele[10]=”todo”;
ele[11]=”hsdiploma”;
ele[12]=”yearofhs”;
ele[13]=”ged”;
ele[14]=”yearofgrad”;
ele[15]=”leadsource”;

for(i=0;i<ele.length;i++)
{
if(document.contact.ele[i].value==””)
{
count=1;
msg=msg+”n”+document.contact.ele[i].name;
}
else if((i>ele.length) && (count==0))
{
return(true);
}
}
for(i=0;i<ele.length;i++)
{
if(document.contact.ele[i].value==””)
{
alert(“PLEASE FILL IN THE FOLLOWING FIELD(S)n “+msg);
document.contact.ele[i].focus();
return(false);
}

}

}
[/code]

submit button as onsubmit=”return validateForm();”>

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@KorMay 13.2008 — [B]ele [/B]is a variable, not an element's name. And [B]return[/B] stops the code, thus in your code you will receive always only [B]return true[/B]

Try this (untested):
<i>
</i>&lt;script type="text/javascript"&gt;
function validateForm(){
var msg='';
var ele=[
"firstname",
"lastname",
"address1",
"city",
"state",
"zip",
"email",
"dayphone",
"programofinterest",
"campus",
"todo",
"hsdiploma",
"yearofhs",
"ged",
"yearofgrad",
"leadsource"
];
for(var i=0;i&lt;ele.length;i++){
document.contact[ele[i]].value==''?msg+=(ele[i]+','):null;
}
if(msg.length&gt;0){
var firstName=msg.split(', ')[0];
alert("PLEASE FILL IN THE FOLLOWING FIELD(S)n "+msg);
document.contact[firstName].focus();
return false;
}
}
&lt;/script&gt;
×

Success!

Help @smilesmita spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...