/    Sign up×
Community /Pin to ProfileBookmark

String concatenation through functions

Hello,

I am attempting to pass a string through several functions in JavaScript to be concatenated.

For some reason, the first string concatenated works fine and the second string is not concatenated.

My page is at:
[url]http://gis38.exp.sis.pitt.edu/is2770/guest.html[/url]

I am trying to pass the erroStr variable to two functions in my validate function. When the first name and email are entered incorrectly, the first name is the only string shown in the alert box. I can’t get both the first name and email error messages to appear in the alert box. Why are they both not appearing?

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@Dan_DrillichFeb 13.2003 — Please change -

<i>
</i>accept = accept &amp;&amp; valfName(theForm.fName, true);
accept = accept &amp;&amp; vallName(theForm.lName, true);
accept = accept &amp;&amp; valEmail(theForm.email, true);


to -
<i>
</i>accept1 = valfName(theForm.fName, true);
accept2 = vallName(theForm.lName, true);
accept3 = valEmail(theForm.email, true);
accept = accept1 &amp;&amp; accept2 &amp;&amp; accept3;


Cheers,

Dan
Copy linkTweet thisAlerts:
@VanJay011379authorFeb 13.2003 — That was exactly what I wanted! Thanks guys.

Dave, I'll have to examine the code myself. I didn't think changing the boolean variables like this would solve the problem but it did ; )
Copy linkTweet thisAlerts:
@Dan_DrillichFeb 13.2003 — Let's look at this code segment -

accept = accept && valfName(theForm.fName, true);

accept = accept && vallName(theForm.lName, true);

accept = accept && valEmail(theForm.email, true);

Let's say the first line evaluated to false;

The second line is -

accept = accept && vallName(theForm.lName, true);

which is actually -

accept = false && vallName(theForm.lName, true);

Now, the interpreter has no reason to execute the function vallName, since 'false && false/true' will evaluate to false.

So, the function vallName is not being called.

Good night guys ?

Dan
Copy linkTweet thisAlerts:
@Dan_DrillichFeb 13.2003 — BTW, there is a need here to improve the regular expressions.

For example, a better one for the last name can be /^([a-zA-Z]+)$/; so a last name like Michael12 won't slip through.

You might also consider the apostrophe to be a valid character, so a person by the name O'connor will be valid.
Copy linkTweet thisAlerts:
@Dan_DrillichFeb 14.2003 — “JavaScript the definitive guide” says the following about logical and (&&) –

The actual behavior of this operator is somewhat more complicated. It starts by evaluating its first operand, the expression on its left. If the value of this expression can be converted to false, the operator returns the value of the lefthand expression. Otherwise, it evaluates its second operand, the expression on its right, and returns the value of that expression.

Note that, depending on the value of the lefthand expression, this operator may or may not evaluate the righthand expression. You may occasionally see code that purposely exploits this feature of the && operator. For example, the following two lines of JavaScript code have equivalent effects:

if (a==b) stop();

(a==b) && stop();

While some programmers (particularly Perl programmers) find this a natural and useful programming idiom, I recommend against it. The fact that the righthand side is not guaranteed to be evaluated is a frequent source of bugs. Consider the following code, for example:

if ((a == null) && (b++ > 10)) stop();

This statement probably does not do what the programmer intended, since the increment operator on the righthand side is not evaluated whenever the comparison on the left side is false. To avoid this problem, do not use expressions with side effects (assignments, increments, decrements, and function calls) on the righthand side of && unless you are quite sure you know exactly what you are doing.[/quote]


Cheers,

Dan
Copy linkTweet thisAlerts:
@VanJay011379authorFeb 14.2003 — [i]Originally posted by Dan Drillich [/i]

[B]

Now, the interpreter has no reason to execute the function vallName, since 'false && false/true' will evaluate to false.

So, the function vallName is not being called.



Good night guys ?

Dan [/B]
[/QUOTE]


Oh, the short circuit of the boolean. Ohhhhhh . . . . . :eek:

Thanks!:p
×

Success!

Help @VanJay011379 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.3,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...