Newbie problem checking input fields

  • Thread starter DaveForgotHisEngineering
  • Start date
D

DaveForgotHisEngineering

I'm haven't worked with javascript in 4 years so I was wondering if
someone could help me. I've written some javascript that is supposed
to check if the input fields have been filled in and display an alert
if they are not. I've run my javascript through an online javascript
checker and it says the code is ok. However, the alert message
doesn't display when the user clicks submit and the fields aren't
filled in. I suspect that the function isEmpty() is not being run
when the user clicks submit. Thanks in advance for any suggestions
you can offer.

(Code snippet from top of page)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<head>
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1" />
<!-- InstanceBeginEditable name="doctitle" -->
<title>GEC Consultants experts in restaurant development, restaurant
profitability
and marketing and expert witness in food services</title>
<!-- InstanceEndEditable -->
<script language="JavaScript" type="text/javascript">
<!--
function MM_reloadPage(init) { //reloads the window if Nav4 resized
if (init==true) with (navigator) {if
((appName=="Netscape")&&(parseInt(appVersion)==4)) {
document.MM_pgW=innerWidth; document.MM_pgH=innerHeight;
onresize=MM_reloadPage; }}
else if (innerWidth!=document.MM_pgW || innerHeight!
=document.MM_pgH) location.reload();
}
function isEmpty()
{
var a=document.getElementsByName("Name:").value.length;
var b=document.getElementsByName("Email:").value.length;
var c=document.getElementsByName("Telephone:").value.length;
var d=document.getElementsByName("City:").value.length;
var e=document.getElementsByName("State:").value.length;
var f=document.getElementsByName("Comments").value.length;

if(!a || !b || !c || !d || !e || !f)
{
alert("Please fill in all of the input fields and comments
section");
}
}
MM_reloadPage(true);
//-->
</script>
..
..
..
..
(Code snippet by submit button)
<h2>Please include a short story about your restaurant:</h2>
<p class="style1">
<label>
<textarea name="Comments" cols="60" rows="10"></textarea>
</label>
</p>
<p>&nbsp;</p>
<p>
<input type="submit" name="Submit" value="Submit"
onclick-"isEmpty()"/>
</p>
 
S

SAM

DaveForgotHisEngineering a écrit :
I suspect that the function isEmpty() is not being run
when the user clicks submit. Thanks in advance for any suggestions
you can offer.
function isEmpty()
{
var a=document.getElementsByName("Name:").value.length;
var b=document.getElementsByName("Email:").value.length;
var c=document.getElementsByName("Telephone:").value.length;
var d=document.getElementsByName("City:").value.length;
var e=document.getElementsByName("State:").value.length;
var f=document.getElementsByName("Comments").value.length;

// and where do you look if the elements's values are empty ?

var a=document.getElementsByName("Name:").value.length>0;
var b=document.getElementsByName("Email:").value.length>0;
var c=document.getElementsByName("Telephone:").value.length>0;
var d=document.getElementsByName("City:").value.length>0;
var e=document.getElementsByName("State:").value.length>0;
var f=document.getElementsByName("Comments").value.length>0;
if(!a || !b || !c || !d || !e || !f)
{
alert("Please fill in all of the fields and comments");

return false;

return true;
}
</script>
.
.
.
.
(Code snippet by submit button)

<h2>Please include a short story about your restaurant:</h2>
<p class="style1">
<label>
<textarea name="Comments" cols="60" rows="10"></textarea>
</label>
</p>
<p>&nbsp;</p>
<p>
<input type="submit" name="Submit" value="Submit"
onclick-"isEmpty()"/>

</form>
 
T

Thomas 'PointedEars' Lahn

DaveForgotHisEngineering said:
I'm haven't worked with javascript in 4 years so I was wondering if
someone could help me. I've written some javascript that is supposed to
check if the input fields have been filled in and display an alert if
they are not. I've run my javascript through an online javascript
checker and it says the code is ok.

The script code is *syntactically* correct (when ignoring non-standard
syntax), although it might not have been recognized due to the comment.

See below.
However, the alert message doesn't display when the user clicks submit
and the fields aren't filled in.

BAD. Broken as designed.
I suspect that the function isEmpty() is not being run when the user
clicks submit.

Yes, it is being run.
[...] <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html
xmlns="http://www.w3.org/1999/xhtml"><!-- InstanceBegin
template="/Templates/GEC-inside.dwt" codeOutsideHTMLIsLocked="false" --
<head>

There it starts to become invalid, because the comment ended (`--'),
but the declaration (`<!') did not (no `>' before `<head>').

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"
/>

It can be either XHTML or HTML.
<!-- InstanceBeginEditable name="doctitle" -->

Superfluous. Whatever template engine you are using, it should not generate
such comments in client-side code.
<title>GEC Consultants experts in restaurant development, restaurant
profitability and marketing and expert witness in food services</title>
<!-- InstanceEndEditable -->

See above.
<script language="JavaScript" type="text/javascript">

The `language' attribute is deprecated since HTML 4.0 (1998 CE) as it is
unnecessary and potentially harmful (with certain values); remove it.

Since the XHTML `script' element has content model PCDATA, this would
comment out the entire `script' element, leaving an empty <script/>.
If it was HTML where the `script' element content model is CDATA, it would
be passed to the script engine as is, and may result in a syntax error.
This technique is obsolete at least since 2000 CE (when HTML 2.0 was
declared obsolete by RFC2854); remove it.
function MM_reloadPage(init) { //reloads the window if Nav4 resized if
(init==true) with (navigator) {if
((appName=="Netscape")&&(parseInt(appVersion)==4)) {
document.MM_pgW=innerWidth; document.MM_pgH=innerHeight;
onresize=MM_reloadPage; }} else if (innerWidth!=document.MM_pgW ||
innerHeight! =document.MM_pgH) location.reload(); }

Remove this junk. Do not use Macromedia/Adobe scripts without complete
informed rewrite, if that.
function isEmpty()
{
var a=document.getElementsByName("Name:").value.length;

The identifier is `getElement*s*ByName' for a reason; a NodeList object
reference is returned as names do not need to be unique in a document.
Since a NodeList object would not have a `value' property and `undefined'
has no properties, you get a runtime error here; look into your console.
[...] if(!a || !b || !c || !d || !e || !f)

if (!(a && b && c && d && e && f))

is equivalent (De Morgan's theorem), but more efficient (shortcut evaluation
in ECMAScript implementations). Space should be used after keywords for
control statements, to distinguish them from function calls.
{ alert("Please fill in all of the input fields and comments section");

I recommend to use window.alert() as this method is a property of Window
objects, not just any possible object in the scope chain. See previous
discussions.
} }
MM_reloadPage(true);
//-->

Remove both lines.
</script> [...] (Code snippet by submit button)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Parse error.
<h2>Please include a short story about your restaurant:</h2>

This should not be a heading. Use CSS to increase font size and weight, not
structural markup.
<p class="style1">

This should be a `div' element, as there is no running text for a paragraph.
<label> <textarea name="Comments" cols="60" rows="10"></textarea>
</label>

The `label' element is pointless here as you don't include a label text.
I recommend to use lowercase names.
</p> <p>&nbsp;</p>

Remove empty paragraphs; use CSS (since 1996-12 CE) to define margins instead.
<p> <input type="submit" name="Submit" value="Submit"
onclick-"isEmpty()"/> </p>

See above.


PointedEars
 
D

Dr J R Stockton

In comp.lang.javascript message <5383f3d9-d788-40e3-941f-54b00ebd4895@i2
4g2000prf.googlegroups.com>, Tue, 26 Aug 2008 11:27:01,
DaveForgotHisEngineering said:
I'm haven't worked with javascript in 4 years so I was wondering if
someone could help me. I've written some javascript that is supposed
to check if the input fields have been filled in and display an alert
if they are not. I've run my javascript through an online javascript
checker and it says the code is ok.

You should say clearly (URL) which checker.
However, the alert message
doesn't display when the user clicks submit and the fields aren't
filled in.

You should say which browser and OS.
I suspect that the function isEmpty() is not being run
when the user clicks submit.

Then put an alert in it to determine whether it is being called.
Thanks in advance for any suggestions
you can offer.

(Code snippet from top of page)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<head>
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1" />
<!-- InstanceBeginEditable name="doctitle" -->
<title>GEC Consultants experts in restaurant development, restaurant
profitability
and marketing and expert witness in food services</title>
<!-- InstanceEndEditable -->
<script language="JavaScript" type="text/javascript">
<!--
function MM_reloadPage(init) { //reloads the window if Nav4 resized
if (init==true) with (navigator) {if
((appName=="Netscape")&&(parseInt(appVersion)==4)) {
document.MM_pgW=innerWidth; document.MM_pgH=innerHeight;
onresize=MM_reloadPage; }}
else if (innerWidth!=document.MM_pgW || innerHeight!
=document.MM_pgH) location.reload();
}

Before posting to News, remove what is irrelevant and re-test to see
that the question remains. I doubt whether any interest in NN4 remains.
Also, read the FAQ.
function isEmpty()
{
var a=document.getElementsByName("Name:").value.length;
var b=document.getElementsByName("Email:").value.length;
var c=document.getElementsByName("Telephone:").value.length;
var d=document.getElementsByName("City:").value.length;
var e=document.getElementsByName("State:").value.length;
var f=document.getElementsByName("Comments").value.length;

if(!a || !b || !c || !d || !e || !f)
{
alert("Please fill in all of the input fields and comments
section");
}
}

You could more simply multiply the lengths together; only if any are
empty will you get zero. And ' = ""' is slightly shorter than
'.length', and probably makes the code more understandable.

Use of 'init==true' rather than 'init' is a sign of a feeble-minded
programmer.

It's a good idea to read the newsgroup c.l.j and its FAQ. See below.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top