Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-15-2012, 01:17 PM   PM User | #1
CodyJava
New Coder

 
Join Date: Sep 2012
Posts: 25
Thanks: 21
Thanked 0 Times in 0 Posts
CodyJava is an unknown quantity at this point
Trouble with onsubmit event in form

I'm trying to test some input boxes on submit, but its not working for some reason I tried playing with the action and method in the form with no luck. Maybe its my script but i couldn't find where. It should run the script on click of the submit. Thanks


Code:
<!DOCTYPE HTML>
<html>
<head>
<title>Chapter 11</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<script type="text/javascript">
function submit(){
if(document.forms[0].visitor_name.value == "" || document.forms[0].visitor_name.value == "Enter your name"){
window.alert("You did not enter your name");
return false;
}else if(document.forms[0].email.value == ""){
window.alert("You did not enter your email address");
return false;
}else if(document.forms[0].pass1.value != document.forms[0].pass2.value || 
document.forms[0].pass1.value == "" || document.forms[0].pass2.value == ""){
window.alert("Your passwords do not match");
}
return true;
}
</script>
</head>
<body>
<h1>Web Site Registration Form</h1>
<form action="" method="post" onsubmit="return submit()">
<h2>Personal Information</h2>
<p>Name
<br>
<br>
<input type="text" value="Enter your name" name="visitor_name" 
onclick="if (this.value == 'Enter your name') this.value = '';"
size="50">
<br>
<br>
E-mail address
<br>
<br>
<input type="text" value="Enter your e-mail address" name="email" 
onclick="if (this.value == 'Enter your e-mail address') this.value = '';"
size="50">
<br>
</p>
<h2>Security Information</h2>
<p>Enter a password that you can use to manage your subscription online
<br>
<br>
<input type="text" name="pass1" value="" size="50">
<!-- onblur??? -->
<br>
<br>
Type the password again to confirm it
<br>
<br>
<input type="text" name="pass2" value="" size="50">
<br>
<br>
Security question
<br>
<br>
<select name="question">
<option value="mother">What is your mother's maiden name?</option>
<option value="pet">What is the name of pet?</option>
<option value="color">What is your favorite color?</option>
</select>
<br>
<br>
Your answer
<br>
<br>
<input type="text" name="ans" size="50">
<br>
</p>
<h2>Preferences</h2>
<p>Send special offers to my e-mail address
<input type="radio">
<input type="radio">
<br>
<br>
Select areas of interest(select at least one)
<br>
<br>
<input type="checkbox">
<br>
<input type="checkbox">
<br>
<input type="checkbox">
<br>
<input type="checkbox">
<br>
<input type="checkbox">
<br>
<br>
</p>
<p><input type="submit" /></p>
</form>
</body>
</html>

Thanks again.

Last edited by CodyJava; 11-15-2012 at 02:01 PM..
CodyJava is offline   Reply With Quote
Old 11-15-2012, 01:55 PM   PM User | #2
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,037
Thanks: 197
Thanked 2,411 Times in 2,389 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
When posting here please help us to help you by following the posting guidelines and wrapping your code in CODE tags. This means use the octothorpe or # button on the toolbar. You can (and should) edit your previous post.

Form validation of the pattern if (document.formname.formfield.value == "") - that is blank - is barely worthy of the name, and virtually useless, as even a single space, an X or a ? will return false, that is pass the validation. A proper name may only contain letters, hyphen, space and apostrophe.
Numeric values, such as zip codes and phone numbers, should be validated as such. Ditto email addresses. This topic has been covered many times before in this forum.

function submit(){

You should avoid giving names or id's to your variables/functions/arguments/forms words which are HTML/JavaScript methods/properties/attributes such as 'name' or 'id' or 'value' or 'test' or 'text' or 'checked' or 'click' or 'href' or 'closed' or 'go' or 'submit' or 'replace' or 'button' or 'radio' or 'parseInt'.

Instead of repeating ad nauseam document.forms[0] just create a new variable
var f = document.forms[0];


Why does it not work? Hint - look for missing closing braces.


All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.

Last edited by Philip M; 11-15-2012 at 02:02 PM..
Philip M is offline   Reply With Quote
Users who have thanked Philip M for this post:
CodyJava (11-15-2012)
Old 11-15-2012, 02:01 PM   PM User | #3
CodyJava
New Coder

 
Join Date: Sep 2012
Posts: 25
Thanks: 21
Thanked 0 Times in 0 Posts
CodyJava is an unknown quantity at this point
its a homework assignment and we were suppose to call it submit() so no answer on why it doesn't work just critique?
CodyJava is offline   Reply With Quote
Old 11-15-2012, 02:08 PM   PM User | #4
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,037
Thanks: 197
Thanked 2,411 Times in 2,389 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Quote:
Originally Posted by CodyJava View Post
its a homework assignment and we were suppose to call it submit() so no answer on why it doesn't work just critique?
I said - Why does it not work? Hint - look for missing closing braces.

We are not here to do your homework for you.

If your teacher has told you to name a function submit() - get a new teacher!
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
Philip M is offline   Reply With Quote
Users who have thanked Philip M for this post:
CodyJava (11-15-2012)
Old 11-15-2012, 02:14 PM   PM User | #5
CodyJava
New Coder

 
Join Date: Sep 2012
Posts: 25
Thanks: 21
Thanked 0 Times in 0 Posts
CodyJava is an unknown quantity at this point
I checked all my brackets and it validates, also I posted that before you edited your post.
Also I typed all that code you did not all I need is help with a problem, what is this forum for if you can't get help.
CodyJava is offline   Reply With Quote
Old 11-15-2012, 06:45 PM   PM User | #6
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,037
Thanks: 197
Thanked 2,411 Times in 2,389 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Did you rename the function submit() to something else?

I now see that you have indeed placed closing braces on the same line as the next statement.
}else if(document.forms[0].email.value == ""){
Not recommended for clarity!

Do not use else if in this context. Each if statement is independent and not contingent on a previous one.

Code:
<script type="text/javascript">

function sub(){
if(document.forms[0].visitor_name.value == "" || document.forms[0].visitor_name.value == "Enter your name"){
window.alert("You did not enter your name");
return false;a 
}
if ((document.forms[0].email.value == "") || (document.forms[0].email.value == "Enter your e-mail address")) {
window.alert("You did not enter your email address");
return false;
}
if(document.forms[0].pass1.value != document.forms[0].pass2.value || 
document.forms[0].pass1.value == "" || document.forms[0].pass2.value == ""){
window.alert("Your passwords do not match");
return false;
}
return true;
}
</script>
But as I have pointed out this sort of form validation (check if a field is blank) is virtually useless. A single space will return false.
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.

Last edited by Philip M; 11-15-2012 at 06:53 PM..
Philip M is offline   Reply With Quote
Users who have thanked Philip M for this post:
CodyJava (11-15-2012)
Old 11-15-2012, 07:11 PM   PM User | #7
minder
Banned

 
Join Date: Oct 2012
Posts: 81
Thanks: 0
Thanked 4 Times in 4 Posts
minder can only hope to improve
Quote:
Originally Posted by Philip M View Post

[CODE]<script type="text/javascript">
...
return false;a
....
Your signature says

Quote:
All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
so what is the red a in your code for ?
minder is offline   Reply With Quote
Old 11-15-2012, 07:24 PM   PM User | #8
CodyJava
New Coder

 
Join Date: Sep 2012
Posts: 25
Thanks: 21
Thanked 0 Times in 0 Posts
CodyJava is an unknown quantity at this point
Thanks I changed the name of the function and used separate if statements and it worked. The assignment was to display an error message if nothing is entered in the input boxes that's why I left them blank, otherwise I wouldn't leave them blank. Thanks for your help.
CodyJava is offline   Reply With Quote
Old 11-15-2012, 07:46 PM   PM User | #9
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,037
Thanks: 197
Thanked 2,411 Times in 2,389 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Quote:
Originally Posted by minder View Post
Your signature says



so what is the red a in your code for ?
No idea how that got there, bullant. It is not in the code I tested. I must have pressed the a key instead of something else when I added my remark about form validation. But thank you for pointing it out - it might have caused a problem for CodyJava.
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.

Last edited by Philip M; 11-15-2012 at 07:48 PM..
Philip M is offline   Reply With Quote
Old 11-15-2012, 07:49 PM   PM User | #10
minder
Banned

 
Join Date: Oct 2012
Posts: 81
Thanks: 0
Thanked 4 Times in 4 Posts
minder can only hope to improve
Quote:
Originally Posted by Philip M View Post
It is not in the code I tested.
yeah yeah, whatever
minder is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:12 PM.


Advertisement
Log in to turn off these ads.