473,770 Members | 7,213 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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="retur n 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 23396
"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*****@litote s.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/rasterTriangleD OM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #3

"Richard Cornford" <Ri*****@litote s.demon.co.uk> wrote in message
news:bq******** ***********@new s.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*****@litote s.demon.co.uk> wrote in message
news:bq******** ***********@new s.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*****@litote s.demon.co.uk> wrote in message
news:bq******** ***********@new s.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="retur n 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/rasterTriangleD OM.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="retur n 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/rasterTriangleD OM.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="retur n 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="retur n 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.element s['someElement'].value ) {
// do some stuff
}
}

....and it's form:

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

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

Now compare it to this function:

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

....and it's form:

<FORM action="..." method="..." name="myForm"
onsubmit="retur n 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.******@blueyo nder.co.uk.invalid (remove ".invalid" to reply)
Jul 20 '05 #10

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

Similar topics

3
8899
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 a empty value instead of returning the browser type. Here is the line which i am using in my code and from manual: <?php echo $_SERVER; ?>
2
26709
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 empty elements in the XML. How to do that ? I don't understand why there is no simple option to specify the output format, or did I miss something ? regards andreas
11
4608
by: Dan Bass | last post by:
which one do you use and why? MyString == null || MyString == "" vs MyString == null || MyString.Length == 0
4
2349
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 String; x="111"; x="222"; x="333";
14
2350
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
3812
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 test whether a string has a value or not by the following syntax: if (thisString.Trim() == "") {
26
2795
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
3674
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 = string.Empty; // do this string myString; // do not do this Questions 1. Is that a good rule? 2. If so, why? If not, why not?
2
14198
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 tool, empty string gets written as NUL character. Is there any way I can bcp out empty string as empty string itself instead of NUL to the file? The bcp command I used to export table into csv file is bcp "SELECT...
21
2980
by: Sami | last post by:
string = "" or string = string.Empty? should is the proper way? Sami
0
9592
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9425
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10058
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10004
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7416
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6678
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5450
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3972
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3576
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.