/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Form Validation problem.

So I modified a script from javascript.internet.com to do what I need and it works perfectly when an answer is skipped/empty however it is still showing the alert even when the fields are filled in. When an answer is missing it properly shows what is missing in the alert box, however when everything is filled in it shows the base alert message. Here is the code please tell me what I did wrong, as I build the function dynamically with php.

[code=html]
<html>
<head>
<script type=”text/javascript”>
function verify() {
var themessage = “You are required to complete all fields: “;
if ( (document.socialinfo.INFO1[0].checked==false) && (document.socialinfo.INFO1[1].checked==false) ) {
themessage = themessage + ” – Submit Anonymously”;
}

if (document.socialinfo.INFO6.value==””) {
themessage = themessage + ” – Agency Name”;
}
if ( (document.socialinfo.INFO7[0].checked==false) && (document.socialinfo.INFO7[1].checked==false) && (document.socialinfo.INFO7[2].checked==false) && (document.socialinfo.INFO7[3].checked==false) && (document.socialinfo.INFO7[4].checked==false) && (document.socialinfo.INFO7[5].checked==false) ) {
themessage = themessage + ” – Agency Type”;
}

//alert if fields are empty and cancel form submit
if (themessage == “You are required to complete the following fields: “) {
document.socialinfo.submit();
}
else {
alert(themessage);
return false;
}
}
</script>
</head>
<body>
<form action=”social_steps.php?step=2″ method=”post” name=”socialinfo”>
<table>
<tr>
<td>Submit Anonymously</td>
<td>
<input type=”radio” name=”INFO1″ value=”yes” onclick=”anon();” /> Yes<br />
<input type=”radio” name=”INFO1″ value=”no” onclick=”anon();” /> No
</td>

</tr>
<tr>
<td>First Name</td>
<td><input type=”text” name=”INFO2″ /></td>
</tr>

<tr>
<td>Last Name</td>
<td><input type=”text” name=”INFO3″ /></td>

</tr>

<tr>
<td>E-Mail</td>
<td><input type=”text” name=”INFO4″ /></td>
</tr>

<tr>
<td>Phone Number</td>
<td><input type=”text” name=”INFO5″ /></td>

</tr>

<tr>
<td>Agency</td>
<td><input type=”text” name=”INFO6″ /></td>
</tr>

<tr>
<td>What best describes your agency?</td>
<td>

<input type=”radio” name=”INFO7″ value=”Child Welfare (public)” /> Child Welfare (public)<br />
<input type=”radio” name=”INFO7″ value=”Private, non profit” /> Private, non profit<br />
<input type=”radio” name=”INFO7″ value=”Juvenile Court/Youth Services” /> Juvenile Court/Youth Services<br />
<input type=”radio” name=”INFO7″ value=”Association/Project” /> Association/Project<br />
<input type=”radio” name=”INFO7″ value=”Mental Health” /> Mental Health<br />

<input type=”radio” name=”INFO7″ value=”Other” /> Other<br />
</td>
</tr>
<tr>
<td colspan=”2″ align=”center”>
<input type=button value=”Next Step >>” onclick=”verify();” />
</td>
</tr>

</table>
</form>
</body></html>
[/code]

NVM I just realized I failed to type the same base message in the check area.

to post a comment
JavaScript

0Be the first to comment 😎

×

Success!

Help @NoasIT 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.27,
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,
)...