/    Sign up×
Community /Pin to ProfileBookmark

What’s wrong with my form validation script?

[CODE]<script type=”text/javascript”>
function validate() {
if (document.UploadForm.SongName.value == “”) {
window.alert(“Oops, you forgot to enter a Song Name.”);
return false;
}
if (document.UploadForm.ArtistName.value == “”) {
window.alert(“Oops, you forgot to enter an Artist or Band Name.”);
return false;
}
if (document.UploadForm.Genre.value == “”) {
window.alert(“Oops, you forgot to enter a genre for the song.”);
return false;
}
return true; window.open(‘form.htm’,”,’width=370,height=115′, true);
}
</script>[/CODE]

The above is my code to validate a form of mine. When everything validates sucessfully, then I want it to open a popup window as well as continue the form’s Action. I know that I did something wrong towards the end. Can anybody find it? Thanks in advance!!

to post a comment
JavaScript

9 Comments(s)

Copy linkTweet thisAlerts:
@Arty_EffemSep 05.2006 — [CODE]<script type="text/javascript">
function validate() {
if (document.UploadForm.SongName.value == "") {
window.alert("Oops, you forgot to enter a Song Name.");
return false;
}
if (document.UploadForm.ArtistName.value == "") {
window.alert("Oops, you forgot to enter an Artist or Band Name.");
return false;
}
if (document.UploadForm.Genre.value == "") {
window.alert("Oops, you forgot to enter a genre for the song.");
return false;
}
return true; window.open('form.htm','','width=370,height=115', true);
}
</script>[/CODE]


The above is my code to validate a form of mine. When everything validates sucessfully, then I want it to open a popup window as well as continue the form's Action. I know that I did something wrong towards the end. Can anybody find it? Thanks in advance!![/QUOTE]
Clue: You make prolific use of the [I]return[/I] statement, yet you don't seem to appreciate what it does...

Also I'm not certain that popup will be classed as requested, and checking for a zero-length string constitutes the poorest standard of validation.

You're welcome in advance.
Copy linkTweet thisAlerts:
@CharlesSep 05.2006 — &lt;script type="text/javascript"&gt;
function validate(f) {
if (!/S/.test(f.SongName.value)) {
window.alert("Oops, you forgot to enter a Song Name.")
return false
}
if (!/S/.test(f.ArtistName.value)) {
window.alert("Oops, you forgot to enter an Artist or Band Name.")
return false;
}
if (!/S/.test(f.Genre.value)) {
window.alert("Oops, you forgot to enter a genre for the song.");
return false
}
window.open ('', 'child', 'width=370,height=115', true)
}
&lt;/script&gt;


&lt;form action="form.html" onsubmit="return validate (this)" target="child"&gt;
Copy linkTweet thisAlerts:
@goofy1989authorSep 05.2006 — Thanks for the advise Arty Effem and thanks for the example Charles, but it doesn't work. When you submit the form, it validates properly, but the window isn't created.

Also, Charles, in your example, can you explain what the new pieces do in this part:
[CODE] if (!/S/.test(f.SongName.value)) {[/CODE]
Specifically like what the [I]!/S/[/I] means? Thanks!
Copy linkTweet thisAlerts:
@CharlesSep 06.2006 — That evaluates to true if there is no non-white space character in the field.

I had a wee type-o in my example above and I've corrected it.
Copy linkTweet thisAlerts:
@JMRKERSep 06.2006 — In the following line:
[code=php]
return true; window.open('form.htm','','width=370,height=115', true);
[/code]

it appears to me that you are returning from the function

before it ever gets a chance to open the window.

Hence, the proper validation, but no pop-up window.
Copy linkTweet thisAlerts:
@goofy1989authorSep 08.2006 — I don't think you guys fully understand what I'm asking, but thanks all to your replies.

I need the form to go through a normal "action" page when submitted, as well as a popup window opening that doesn't have anything to do with the form contents. I don't need to form to be submitted into a popup window. I hope that cleared some things up.
Copy linkTweet thisAlerts:
@CharlesSep 08.2006 — Well, that does change things. But I need a little more clarification. What do you want to happen for those who block pop-ups or don't use JavaScript?
Copy linkTweet thisAlerts:
@1andywSep 08.2006 — I don't think you guys fully understand what I'm asking, but thanks all to your replies.

I need the form to go through a normal "action" page when submitted, as well as a popup window opening that doesn't have anything to do with the form contents. I don't need to form to be submitted into a popup window. I hope that cleared some things up.[/QUOTE]


I have used a 'redirect after submit' to good advantage. Will this work for you?

<code>

<input type="hidden"

name="redirect"

value="http://www.yoursite.org/Acknowledgement.html">

</code>
Copy linkTweet thisAlerts:
@goofy1989authorSep 18.2006 — Well, that does change things. But I need a little more clarification. What do you want to happen for those who block pop-ups or don't use JavaScript?[/QUOTE]

Well I am using ASP Upload to upload filed to my server. ASP Upload has a progress window funtion (read about it on aspupload.com). I want to use the progress window, as well as a form validation script.

The progress window uses the onSubmit part of the form, but then how would I also have the validation script work as well? I hope that cleared things up again.
×

Success!

Help @goofy1989 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.16,
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,
)...