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 12-17-2012, 10:13 AM   PM User | #1
Coding123
New to the CF scene

 
Join Date: Dec 2012
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Coding123 is an unknown quantity at this point
Unhappy JavaScript Validation Error.

I have a code with me, and everything works fine (JavaScript) excluding the validation; I think I may have made an error somewhere which could possibly relate to the curly braces I've used. I was wondering if anybody could correct this code. I'm using Notepad++, if that helps.

Code:
<head>
<title>Exam entry</title>

<script language="javascript" type="text/javascript">

function validateForm() {
var result = true;
var msg="";
}

if (document.ExamEntry.name.value=="") {
msg+="You must enter your name \n";
document.ExamEntry.name.focus();
document.getElementById('name').style.color="red";
result = false;
}

if (document.ExamEntry.subject.value=="") {
msg+="You must enter the subject \n";
document.ExamEntry.subject.focus();
document.getElementById('subject').style.color="red";
result = false;
}

if (document.ExamEntry.examination number.value="") {
msg+="You must enter the examination number \n";
document.ExamEntry.examinationnumber.focus();
document.getElementById('examinationnumber').style.color="red";
result = false;
}

if (msg=="") {
return result;
}

{
window.alert(msg)
return result;
}
</script>
</head>
<body>
<p>
<font face="Candara">
<font size="2">
<h1><u>Exam Entry Form</u></h1>
<fieldset>
<form name="ExamEntry" method="post" action="success.html">
<table width="50%" border="0">
<tr>
<td id="name">Name</td>
<td><input type="text" name="name" /></td>
</tr>
<tr>
<td id="subject">Subject</td>
<td><input type="text" name="subject" /></td>
</tr>
<tr>
<tr>
<td id="examination number">Examination Number</td>
<td><input type="text" name="examinationnumber" /></td>
</tr>
<tr>
</tr>
<td><input type="submit" name="Submit" value="Submit" onclick="return validateForm();" /></td>
<td><input type="reset" name="Reset" value="Reset" /></td>
</tr>
</fieldset>
</table>
</form>
</p></font></size></body>
Thanks in advance.

Last edited by Coding123; 12-17-2012 at 10:40 AM..
Coding123 is offline   Reply With Quote
Old 12-17-2012, 11:06 AM   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
Have you tried using your error console?

function validateForm() {
var result = true;
var msg="";
} // end of function

And other errors, including obsolete HTML <font> tags and no !DOCTYPE.

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.


It is your responsibility to die() if necessary….. - PHP Manual
__________________

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; 12-17-2012 at 11:14 AM..
Philip M is online now   Reply With Quote
Old 12-22-2012, 08:10 AM   PM User | #3
vikram1vicky
Regular Coder

 
Join Date: Jul 2011
Location: India
Posts: 496
Thanks: 3
Thanked 57 Times in 56 Posts
vikram1vicky is an unknown quantity at this point
Post ur full code or link where it is hosted.
vikram1vicky is offline   Reply With Quote
Old 12-22-2012, 09:54 AM   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 vikram1vicky View Post
Post ur full code or link where it is hosted.
He has posted his full code.What do you think is missing (apart from <html> and </html>)?
__________________

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 online now   Reply With Quote
Old 12-22-2012, 08:17 PM   PM User | #5
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,454
Thanks: 0
Thanked 498 Times in 490 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
The code looks fine except for the </size> tag. Netscape 2 doesn't understand doctype tags so with the rest of the code written for that browser it is essential then a doctype not be specified.

If targetting more recent browsers such as IE5 the first thing you should do is to rewrite the HTML to a more recent version as the version being used became obsolete in 1997.

If you want it to work properly in modern browsers you should also move the script to just before the </body> tag and get rid of the alert() call as they can no longer be used in live web pages. You should also learn how to use the debugger built into the browser to work out what your JavaScript errors are - as well as feeding your JavaScript through a service such as http://www.jslint.com/ (created by JavaScript expert Douglas Crockford).
__________________
Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/

Last edited by felgall; 12-22-2012 at 08:20 PM..
felgall is offline   Reply With Quote
Reply

Bookmarks

Tags
curly braces, javascript, notepad++, stuck, validation

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 07:21 AM.


Advertisement
Log in to turn off these ads.