/    Sign up×
Community /Pin to ProfileBookmark

Submit onclick goes to TY page without fixing errors

I have a form that if [B]I hit submit it automatically goes to the Thank You/Confirmation page without me fixing the errors[/B]…can anyone tell me how to prevent this? Here’s the code?

[CODE]
<script type=”text/javascript”>

function setReadOnly(style1,style2) {
document.getElementById(“USmail”).disabled=style2;
document.getElementById(“email”).disabled=style1;
}

function validateForm() {
email=document.getElementById(“email”);
USmail=document.getElementById(“USmail”);
gnum=document.getElementById(“gnumber”);
errors=0

if (email.disabled==false){
if (email.value==””){
alert(“please enter an email address”)
errors++
}
p=email.value.indexOf(‘@’);
if (p<1 || p==(email.value.length-1)) {
alert(“please enter a valid email address”)
errors++
}

}

if ((gnum.value.match(/[^0-9]/g))||(gnum.value==””)){
alert(“please enter a valid Group Number”)
errors++
}

if (document.getElementById(“ContactName”).value==””){
alert(“please enter a contact name”)
errors++
}
if (USmail.value==””&&USmail.disabled==false){
alert(“please enter a valid address”)
errors++
}
if (errors==0) {
alert(‘submit form!’)
}
}

</script>

<form id=”cform” class=”form” method=”post” action=”/cform.php”>
<input type=”hidden” name=”settings” value=”/form.php”>

<div>

<div class=”field”>
<p><label for=”gnumber” class=”label”>Group Number<span class=”asterisk”>*</span></label>
<input type=”text” class=”input” name=”gnumber” id=”gnumber” /></p>
</div>

<div class=”field”>
<p><label for=”ContactName” class=”label”>Contact Name<span class=”asterisk”>*</span></label>&nbsp;
<input type=”text” class=”input” name=”ContactName” id=”ContactName” /></p>
</div>

<!–Radio===================–>
<div class=”field”>
<input type=”radio” name=”email” value=”1″ id=”emailrad” checked=”checked” onclick=”setReadOnly(false,true)” />
E-mail (please enter your e-mail address): <input type=”text” class=”input” name=”email” id=”email” /><br /><br />

<input type=”radio” name=”email” value=”2″ id=”usmailrad” onclick=”setReadOnly(true,false)” />
US Mail (please enter your mailing address) <input type=”text” class=”input” disabled=”true” name=”USmail” id=”USmail” />
</div>
<!–Radio===================–>

</div>
<!–Submit–>
<div>
<input name=”submit” type=”submit” value=”Submit” onClick=”validateForm()” />
</div>
<!–Submit–>
</form>

[/CODE]

Thanks in advanced.

to post a comment
JavaScript

7 Comments(s)

Copy linkTweet thisAlerts:
@MarPloOct 11.2011 — Hi,

Try replace this code:
if (errors==0) {
alert('submit form!')
}

With this:
if (errors==0) {
alert('submit form!')
}
else {
return false;
}

Delete [i]onClick="validateForm()"[/i] from Submit button, and add [b]onsubimt="return validateForm()"[/b] in <form>, like this:
&lt;form id="cform" class="form" method="post" action="/cform.php" onsubimt="return validateForm()"&gt;
Copy linkTweet thisAlerts:
@riskmodauthorOct 11.2011 — Hi,

Try replace this code:
if (errors==0) {
alert('submit form!')
}

With this:
if (errors==0) {
alert('submit form!')
}
else {
return false;
}

Delete [i]onClick="validateForm()"[/i] from Submit button, and add [b]onsubimt="return validateForm()"[/b] in <form>, like this:
&lt;form id="cform" class="form" method="post" action="/cform.php" onsubimt="return validateForm()"&gt;[/QUOTE]



SUCCESS THANK YOU!@!! Only thing I am experiencing now is In IE (7 and 8)

are the Radio Buttons! (I hate IE):

[CODE]
if (document.getElementById("ContactName").value==""){

alert("please enter a contact name")
errors++
}
if (USmail.value==""&&USmail.disabled==false){

alert("please enter a valid address")
errors++
}

[/CODE]


Do not work ?...back to the drawing board.
Copy linkTweet thisAlerts:
@MarPloOct 11.2011 — Hy,

Try to replace: [i]if(document.getElementById("ContactName").value=="")[/i]

with:

[b]if(document.getElementById("ContactName").value.length<1)[/b]

The same for the other:

[b]USmail.value.length<1[/b]
Copy linkTweet thisAlerts:
@riskmodauthorOct 11.2011 — Hy,

Try to replace: [i]if(document.getElementById("ContactName").value=="")[/i]

with:

[b]if(document.getElementById("ContactName").value.length<1)[/b]

The same for the other:

[b]USmail.value.length<1[/b][/QUOTE]


Do you mean like this?

[CODE]

if(document.getElementById("ContactName").value.length<1){
alert("please enter a contact name")
errors++
}

if (USmail.value.length<1){

alert("please enter a valid address")
errors++
}
[/CODE]


Still nada but noobness of me is not putting it right
Copy linkTweet thisAlerts:
@riskmodauthorOct 11.2011 — Hy,

Try to replace: [i]if(document.getElementById("ContactName").value=="")[/i]

with:

[b]if(document.getElementById("ContactName").value.length<1)[/b]

The same for the other:

[b]USmail.value.length<1[/b][/QUOTE]


Think this is set:


you can fix it by renaming your buttons. instead of calling them email, just pick another name but make sure it is the same name for both[/QUOTE]


But last thing...If I didn't want a confirmation after submit:

[CODE]
if (errors==0) {
alert('submit form!')
}
else {
return false;
}
[/CODE]


do I just take the alert off?

Thanks!
Copy linkTweet thisAlerts:
@pactor21Oct 11.2011 — [CODE]if(document.getElementById("ContactName").value.length<1){

alert("please enter a contact name")
errors++
}

if (USmail.value.length<1){

alert("please enter a valid address")
errors++
}[/CODE]


For the code above, I don't see any ; meaning a statement is complete. Wouldn't javascript think, in our case, alert and error++ are 1 statement without a semicolon which make no sense?

I might be wrong, but I always put a semicolon at the end of a statement.
Copy linkTweet thisAlerts:
@DanInMAOct 11.2011 — you could just do:
[CODE]if (errors==0) {
return true;
}
else {
return false;
}[/CODE]
×

Success!

Help @riskmod 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 6.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: @nearjob,
tipped: article
amount: 1000 SATS,

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

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...