473,385 Members | 2,013 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

empty string

Hi
I have been busy going through the last weeks postings in an attempt to
absorb javascript syntax (I guess it's not possible to just absorb this
stuff in a passive way - I'm getting way out of my depth with most of the
posts, I will buy a good book and take some online tutorials)

Anyway, I think I almost understand one of Mr Nielsen's posts on form
validation. It all centers around whether I am interpreting an empty string
correctly - see below
Mr Nielsen states:

To validate a function, always use this way to call the validation function:
---
<form ... onsubmit="return validate(this)">
---
The function itself is then:
---
function validate(form) {
var cashEmpty = form.elements['cash'].value == "";
var invoiceEmpty = form.elements['invoice'].value == "";
if (cashEmpty && invoiceEmpty) {
alert("Fill in at least one of the fields: Cash or Invoice.");
return false;
}
return true;
}

================================================== ========================

I think the code above is saying - if the value of the form control named
'cash' has had nothing entered into it by the user, then assign a value of
'zero length string' to the variable called cashEmpty. A zero sized string
is not a null value, so effectively, the empty variable 'cashEmpty' is
flagging 'true' in the if conditional because it has a value (which is the
value 'zero sized string'). Same story for the other form element.

I think I may be making this up. Does javascript see something that is
equivalent of "" as having a value?

Thanks for any reply
David


Jul 20 '05 #1
10 23342
"David Graham" <da************@ntlworld.com> wrote in message
news:HR************@newsfep1-gui.server.ntli.net...
<snip>
| var cashEmpty = form.elements['cash'].value == ""; <snip>I think the code above is saying - if the value of the form
control named 'cash' has had nothing entered into it by the
user, then assign a value of 'zero length string' to the
variable called cashEmpty. A zero sized string is not a null
value, so effectively, the empty variable 'cashEmpty' is
flagging 'true' in the if conditional because it has a value
(which is the value 'zero sized string'). Same story for the
other form element.


No, you have misunderstood. Earlier in the week you may nave noticed
Evertjan commenting on my parenthesising sub-expressions, maybe Lasse
should have done the same to the above. Parenthesising the expression
produces:-

var cashEmpty = (form.elements['cash'].value == "");

- where the result of the parenthesised expression is assigned to the
value of the local variable. The parenthesised expression is a
comparison and a comparison produces a *boolean* true or false result.
So it is that boolean value that is assigned to the local variable. true
if the form element value equals - "" -(in the type-converting
comparison sense[1]) or false if it does not. Parentheses are not
required in the source code because operator precedence would require
that the comparison operation happen prior to the assignment operation
(else it would be uncertain which value to assign to the variable).

Richard.

[1] In addition to the type-converting - == - equality operator, which
would assert that numeric zero, undefined, null and boolean false also
equalled an empty string, recent JavaScript versions have a strict
equality operator - === - which would only believe that an empty string
equalled an empty string. In the above example it doesn't matter much if
the - == - equality operator is used because the value property of form
elements is always a string and the only string that will equal an empty
string is and empty string. Browsers such as IE 4 do not implement the
strict equality operator and consider it a syntax error, but there aren’
t many of those left now.
Jul 20 '05 #2
"Richard Cornford" <Ri*****@litotes.demon.co.uk> writes:
No, you have misunderstood. Earlier in the week you may nave noticed
Evertjan commenting on my parenthesising sub-expressions, maybe Lasse
should have done the same to the above.


Acknowledged.

/L :)
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #3

"Richard Cornford" <Ri*****@litotes.demon.co.uk> wrote in message
news:bq*******************@news.demon.co.uk...
"David Graham" <da************@ntlworld.com> wrote in message
news:HR************@newsfep1-gui.server.ntli.net...
<snip>
| var cashEmpty = form.elements['cash'].value == "";

<snip>
I think the code above is saying - if the value of the form
control named 'cash' has had nothing entered into it by the
user, then assign a value of 'zero length string' to the
variable called cashEmpty. A zero sized string is not a null
value, so effectively, the empty variable 'cashEmpty' is
flagging 'true' in the if conditional because it has a value
(which is the value 'zero sized string'). Same story for the
other form element.


No, you have misunderstood. Earlier in the week you may nave noticed
Evertjan commenting on my parenthesising sub-expressions, maybe Lasse
should have done the same to the above. Parenthesising the expression
produces:-

var cashEmpty = (form.elements['cash'].value == "");

- where the result of the parenthesised expression is assigned to the
value of the local variable. The parenthesised expression is a
comparison and a comparison produces a *boolean* true or false result.
So it is that boolean value that is assigned to the local variable. true
if the form element value equals - "" -(in the type-converting
comparison sense[1]) or false if it does not. Parentheses are not
required in the source code because operator precedence would require
that the comparison operation happen prior to the assignment operation
(else it would be uncertain which value to assign to the variable).

Richard.

[1] In addition to the type-converting - == - equality operator, which
would assert that numeric zero, undefined, null and boolean false also
equalled an empty string, recent JavaScript versions have a strict
equality operator - === - which would only believe that an empty string
equalled an empty string. In the above example it doesn't matter much if
the - == - equality operator is used because the value property of form
elements is always a string and the only string that will equal an empty
string is and empty string. Browsers such as IE 4 do not implement the
strict equality operator and consider it a syntax error, but there aren'
t many of those left now.

Thanks for the explanation - I was thinking way off here, I'm really glad
you posted this.

David
Jul 20 '05 #4

"Richard Cornford" <Ri*****@litotes.demon.co.uk> wrote in message
news:bq*******************@news.demon.co.uk...
"David Graham" <da************@ntlworld.com> wrote in message
news:HR************@newsfep1-gui.server.ntli.net...
<snip>
| var cashEmpty = form.elements['cash'].value == ""; <snip>
I think the code above is saying - if the value of the form
control named 'cash' has had nothing entered into it by the
user, then assign a value of 'zero length string' to the
variable called cashEmpty. A zero sized string is not a null
value, so effectively, the empty variable 'cashEmpty' is
flagging 'true' in the if conditional because it has a value
(which is the value 'zero sized string'). Same story for the
other form element.


No, you have misunderstood. Earlier in the week you may nave noticed
Evertjan commenting on my parenthesising sub-expressions, maybe Lasse
should have done the same to the above. Parenthesising the expression
produces:-

var cashEmpty = (form.elements['cash'].value == "");

Parentheses are not required in the source code because operator precedence would require
that the comparison operation happen prior to the assignment operation
(else it would be uncertain which value to assign to the variable).

Just a quick follow up. Is it best practice to add the parentheses even when
they are not required. I can think that there is some gain to doing this as
a novice such as myself could probably follow the code more easily - or is
it bad to add redundant code full-stop. I am trying to learn good habits and
avoid bad habits at this early stage of learning.
David
Jul 20 '05 #5

"Richard Cornford" <Ri*****@litotes.demon.co.uk> wrote in message
news:bq*******************@news.demon.co.uk...
"David Graham" <da************@ntlworld.com> wrote in message
news:HR************@newsfep1-gui.server.ntli.net...
<snip>
| var cashEmpty = form.elements['cash'].value == ""; <snip>
I think the code above is saying - if the value of the form
control named 'cash' has had nothing entered into it by the
user, then assign a value of 'zero length string' to the
variable called cashEmpty. A zero sized string is not a null
value, so effectively, the empty variable 'cashEmpty' is
flagging 'true' in the if conditional because it has a value
(which is the value 'zero sized string'). Same story for the
other form element.


No, you have misunderstood. Earlier in the week you may nave noticed
Evertjan commenting on my parenthesising sub-expressions, maybe Lasse
should have done the same to the above. Parenthesising the expression
produces:-

var cashEmpty = (form.elements['cash'].value == "");


It has been suggested in this newsgroup that

var cashEmpty = (document.forms['form'].elements['cash'].value == "");

Is a more proper way to refer to the form - presumably, the form would be
named like this

<form name="form" ...........

Is this best practice?

Would the call to the function and the parameter name remain the same i.e.
would the following lines remain as they are now?

<form ... onsubmit="return validate(this)">
---
The function itself is then:
---
function validate(form) {
Again - thanks for any help
David


- where the result of the parenthesised expression is assigned to the
value of the local variable. The parenthesised expression is a
comparison and a comparison produces a *boolean* true or false result.
So it is that boolean value that is assigned to the local variable. true
if the form element value equals - "" -(in the type-converting
comparison sense[1]) or false if it does not. Parentheses are not
required in the source code because operator precedence would require
that the comparison operation happen prior to the assignment operation
(else it would be uncertain which value to assign to the variable).

Richard.

[1] In addition to the type-converting - == - equality operator, which
would assert that numeric zero, undefined, null and boolean false also
equalled an empty string, recent JavaScript versions have a strict
equality operator - === - which would only believe that an empty string
equalled an empty string. In the above example it doesn't matter much if
the - == - equality operator is used because the value property of form
elements is always a string and the only string that will equal an empty
string is and empty string. Browsers such as IE 4 do not implement the
strict equality operator and consider it a syntax error, but there aren'
t many of those left now.

Jul 20 '05 #6
"David Graham" <da************@ntlworld.com> writes:
Just a quick follow up. Is it best practice to add the parentheses even when
they are not required.
Unneeded parentheses are there only to improve readability. Whether you need
them or not depends entirely on who will read it.
I would never us parentheses in a "chain assignment" like
foo = (bar = (baz = 42))
*if* I were writing to people used to a C-like language. It confuzes me :)
But to people who are used to, e.g., BASIC, which some readers of this
group is, it is probably prudent. (I would probably add a note saying
that assignment associates to the right, so the parentheses are not
necessary, so they can learn the traditional way of writing it.).
I can think that there is some gain to doing this as a novice such
as myself could probably follow the code more easily - or is it bad
to add redundant code full-stop.
It's not bad or redundant, just a little extra verbose.
I am trying to learn good habits and avoid bad habits at this early
stage of learning.


Good choice!

I would say: Set the parentheses. Then think about which you can remove.
When you become more fluent, you can do that while you write :)

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #7
"David Graham" <da************@ntlworld.com> writes:
var cashEmpty = (form.elements['cash'].value == "");
It has been suggested in this newsgroup that

var cashEmpty = (document.forms['form'].elements['cash'].value == "");

Is a more proper way to refer to the form - presumably, the form would be
named like this

<form name="form" ...........

Is this best practice?
Using
document.forms['form']
to refer to the form named "form" is infinityle better than just writing
form
(i.e., use the name as a global variable). Among other things, it actually
works in Mozilla.

The document.forms collection is part of the W3C DOM specification, so any
new browser will most likely support it. Any other way to refer to a form
(from scratch) will not have the same official endorsement. Still, just
writing document.form (i.e., the form directly as a property of the document
object) will probably be supported by browsers for a long time.

Now, I said "from scratch". If you already have a reference to the
form, then there is no need to go through hoops to get to it. This is
the case here: the name "form" is not used as a global variable. It is
a local variable, the argument of a function:
function (form) { ... }

So, in this case,
var cashEmpty = (form.elements['cash'].value == "");
is the recommended way of writing it. It's only because *we* have made
the variable "form" point to the form. We don't assume that the browser
has done it for us - that would be wrong.
Would the call to the function and the parameter name remain the same i.e.
would the following lines remain as they are now?

<form ... onsubmit="return validate(this)">
If you find the form from scratch, you don't need the argument to validate.
It is much easier just sending the form reference as an argument, though.
---
The function itself is then:
---
function validate(form) {


Exactly. :)

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #8
Thanks for that reply. I have a lot of learning to do. I inderstood
everything you said, except

<form ... onsubmit="return validate(this)">

If you find the form from scratch, you don't need the argument to validate.
It is much easier just sending the form reference as an argument, though.

The two lines above don't really mean much to me - yet!

Any chance of having another go at rephrasing that (taking into
consideration that this is a bit like teaching my dog the theory of
relativity)

thanks
David


Jul 20 '05 #9
David Graham wrote on 27 Nov 2003:
Thanks for that reply. I have a lot of learning to do. I
inderstood everything you said, except
<form ... onsubmit="return validate(this)">

If you find the form from scratch, you don't need the argument
to validate. It is much easier just sending the form reference
as an argument, though.


The two lines above don't really mean much to me - yet!

Any chance of having another go at rephrasing that (taking into
consideration that this is a bit like teaching my dog the theory
of relativity)


Consider this function:

function validateForm() {
var theForm = document.forms['myForm'];

if ( 'someValue' == theForm.elements['someElement'].value ) {
// do some stuff
}
}

....and it's form:

<FORM action="..." method="..." name="myForm"
onsubmit="return validateForm()">
....
</FORM>

-----------------------------------

Now compare it to this function:

function validateForm( theForm ) {
if ( 'someValue' == theForm.elements['someElement'].value ) {
// do some stuff
}
}

....and it's form:

<FORM action="..." method="..." name="myForm"
onsubmit="return validateForm( this )">
....
</FORM>
With the first snippet, nothing is passed to the function. It has to
create its own reference, then use that to analyse the form.

With the second snippet, the reference to the form is passed (using
the keyword, this) during the call, removing the need to perform the
lookup that the first function required.

It's not much of a difference in this example, but it shows that
passing the reference means less typing at the very least. It can
prevent trivial errors, such as mistyping the form name, and if you
changed the name of the form, the second function doesn't need to be
altered in any way. There might be additional benefits - I'm just not
seeming them at the moment.

That help?

Mike

--
Michael Winter
M.******@blueyonder.co.uk.invalid (remove ".invalid" to reply)
Jul 20 '05 #10
"David Graham" <da************@ntlworld.com> writes:
<form ... onsubmit="return validate(this)">
Any chance of having another go at rephrasing that (taking into
consideration that this is a bit like teaching my dog the theory of
relativity)


The onsubmit handler contains the following code
return validate(this);
It calls the function "validate" with one argument. The code is
evaluated as a method of the form element, so "this" refers directly
to the form. So, the argument is a reference to the form.

At the receiving end, the function is defined as:
function validate(form) { ... }
That means that the local variable "form" will refer directly
to the form. There is no need to find the form using document.forms,
because you already have it. Using just
form.elements['inputName']
is the simplest way to access the input element.

If you *don't* have easy access to the form, you need to use the full
address of the element:
document.forms['formId'].elements['inputName']

The thing that is incorrect is writing:
formId.inputName (broken in Mozilla)
or
document.formId.inputName (works in the browsers I have tested, but is
not part of any standard)

/L
/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: tornado | last post by:
Hi all, I am pretty new to PHP. I was reading PHP manual and trying out the example from 2nd chapter (A simple Tutorial). When i try to print the variable as given in the example it returns...
2
by: Andreas Palm | last post by:
I have a dataset that has DBNull in certain columns, now when I write out this one to XML, I only get the columns as elements that do have data in it. However I do need also the empty colums as...
11
by: Dan Bass | last post by:
which one do you use and why? MyString == null || MyString == "" vs MyString == null || MyString.Length == 0
4
by: web1110 | last post by:
I have an array of of 5 string elements. I put values in 3 of them. Yet when I loop over them, I do not catch the empty string. The code output below does not include "Empty" stringx=new...
14
by: cj | last post by:
What is string.empty used for? I can't say: if string.empty then I have to use: if string = "" then which is ok, I just want to know what .empty is for.
26
by: Neville Lang | last post by:
Hi all, I am having a memory blank at the moment. I have been writing in C# for a number of years and now need to do something in VB.NET, so forgive me such a primitive question. In C#, I...
26
by: anonieko | last post by:
In the past I always used "" everywhere for empty string in my code without a problem. Now, do you think I should use String.Empty instead of "" (at all times) ? Let me know your thoughts.
35
by: Smithers | last post by:
I have been told that it is a good idea to *always* declare string variables with a default value of string.Empty - for cases where an initial value is not known... like this: string myString =...
2
by: Jay | last post by:
I have a SQL Server table with nvarchar type column which has not null constraint. I am inserting empty string ("") from Java to the table column. When I export this table into .csv file using bcp...
21
by: Sami | last post by:
string = "" or string = string.Empty? should is the proper way? Sami
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.