OnLoad form check for empty field

S

simon.croome

Hi,

I am trying to write a simple routine to see if three fields on a form
are empty when the page is refreshed, but I cannot get the example
code below to run at all, can anyone help ?

< My acutal HTML form is called frmCHGTASK but I have already changed
this in attempt to get the code working >

<script language="Javascript">
function checksecure(form)
{
if(document.form.srvname.value == "")
{
alert("Hello World!");
}
</script>

Thanks Simon.
 
B

Bruce Wisentaner

Hi,

I am trying to write a simple routine to see if three fields on a form
are empty when the page is refreshed, but I cannot get the example
code below to run at all, can anyone help ?

< My acutal HTML form is called frmCHGTASK but I have already changed
this in attempt to get the code working >

<script language="Javascript">
function checksecure(form)
{
if(document.form.srvname.value == "")
{
alert("Hello World!");
}
</script>

Huh?
If you control the javascript and you control the HTML, then you know
which fields are prefilled because you are the author.
Is this a Greasemonkey thing?
Anyway, you don't show the form's name, so maybe you should refer to
input field name as document.forms[0].srvname.value.
---Bruce Wisentaner
 
P

Peter Michaux

Huh?
If you control the javascript and you control the HTML, then you know
which fields are prefilled because you are the author.

I wish that was true. Those darn form auto-filler features in browsers
mess up your theory however.
Is this a Greasemonkey thing?
Anyway, you don't show the form's name, so maybe you should refer to
input field name as document.forms[0].srvname.value.

It is brittle to refer to a form by a number in case another for is
inserted in the page ahead of form[0]. More robust ways are

document.forms.myFormName.elements.myInputName.value

document.getElementById('myFormId').elements.myInputName.value

I am in the habit of using the first option. I believe the name
attribute of forms is on it's way out so the second of these options
will be preferred one day. The second option also saves polluting the
window object with an extra variable (the name of the form.)

Peter
 
P

Peter Michaux

Hi,

I am trying to write a simple routine to see if three fields on a form
are empty when the page is refreshed, but I cannot get the example
code below to run at all, can anyone help ?

< My acutal HTML form is called frmCHGTASK but I have already changed
this in attempt to get the code working >

<script language="Javascript">


The language attribute is deprecated. Instead use <script type="text/
javascript">
function checksecure(form)
{
if(document.form.srvname.value == "")

document.myFormName.srvname.value == ''
{
alert("Hello World!");
}
</script>

Peter
 
R

Richard Cornford

Peter Michaux wrote:
It is brittle to refer to a form by a number in case another for is
inserted in the page ahead of form[0]. More robust ways are

document.forms.myFormName.elements.myInputName.value

document.getElementById('myFormId').elements.myInputName.value

I am in the habit of using the first option. I believe the name
attribute of forms is on it's way out so the second of these options
will be preferred one day.

Form elements may be referenced by ID in the - forms - collection so
even if people do act on the deprecation of NAME attributes on form
elements and replace them with IDs then there is still no reason for not
suing the form:-

document.forms.myFormID.elements.myInputName.value
The second option also saves polluting the
window object with an extra variable (the name of the form.)

Where browser make named form elements into properties of the global
object under that name they also almost always make _all_ IDed element
into properties of the global object with property names that correspond
with the ID.

Richard.
 

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

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top