Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > .length is always 1

Reply
Thread Tools

.length is always 1

 
 
maxwells@gmail.com
Guest
Posts: n/a
 
      07-30-2008
Dear Friends,

The following is part of my form validation script. It checks that the
user name entered is in Wiki name format.

function checkup(theform){
var form = document.forms[theform];
var okay = true;
var wikiName = form.name.value.match(/[A-Z][a-z0-9]*[0-9A-Z]/);
if (wikiName.length<3) {
alert(form.name.value + ' is not a WikiName - please use one word
with at least two capital letters');
okay = false;
}
return okay;
}

Normally, if the wikiName returns a match of more than three letters
then a successful match has been returned, so the name is in Wiki
format.

The thing is, even I do enter a valid Wiki name, such as "WikiName",
it is refused. So I put in a couple of debugging alert statements
before the if {} clause, to see what was going on:

alert (wikiName);
alert (wikiName.length);

The result was:

WikiN
1

How can this be? The string is correctly matched, and has 5
characters. So why would its length be reported as 1?

I tried this on Firefox and Explorer, same result both times.

Can anybody solve this mystery?

Thanks for your time,

Dalgetty
 
Reply With Quote
 
 
 
 
Jorge
Guest
Posts: n/a
 
      07-30-2008
On Jul 30, 3:44*pm, maxwe...@gmail.com wrote:
> Dear Friends,
>
> The following is part of my form validation script. It checks that the
> user name entered is in Wiki name format.
>
> function checkup(theform){
> * * * * var form = document.forms[theform];
> * * * * var okay = true;
> * * * * var wikiName = form.name.value.match(/[A-Z][a-z0-9]*[0-9A-Z]/);
> * * * * if (wikiName.length<3) {
> * * * * * * * * alert(form.name.value + ' is not a WikiName - please use one word
> with at least two capital letters');
> * * * * * * * * okay = false;
> * * * * }
> * * * * return okay;
>
> }
>
> Normally, if the wikiName returns a match of more than three letters
> then a successful match has been returned, so the name is in Wiki
> format.
>
> The thing is, even I do enter a valid Wiki name, such as "WikiName",
> it is refused. So I put in a couple of debugging alert statements
> before the if {} clause, to see what was going on:
>
> * * * * alert (wikiName);
> * * * * alert (wikiName.length);
>
> The result was:
>
> * * * * WikiN
> * * * * 1
>
> How can this be? The string is correctly matched, and has 5
> characters. So why would its length be reported as 1?
>
> I tried this on Firefox and Explorer, same result both times.
>
> Can anybody solve this mystery?
>


alert(typeof wikiName) -> "object"

Try:

var wikiName = form.name.value.match(/[A-Z][a-z0-9]*[0-9A-
Z]/).toString();

or

var wikiName = form.name.value.match(/[A-Z][a-z0-9]*[0-9A-Z]/) + "";

--Jorge.
 
Reply With Quote
 
 
 
 
GArlington
Guest
Posts: n/a
 
      07-30-2008
On Jul 30, 2:44 pm, maxwe...@gmail.com wrote:
> Dear Friends,
>
> The following is part of my form validation script. It checks that the
> user name entered is in Wiki name format.
>
> function checkup(theform){
> var form = document.forms[theform];
> var okay = true;
> var wikiName = form.name.value.match(/[A-Z][a-z0-9]*[0-9A-Z]/);
> if (wikiName.length<3) {
> alert(form.name.value + ' is not a WikiName - please use one word
> with at least two capital letters');
> okay = false;
> }
> return okay;
>
> }
>
> Normally, if the wikiName returns a match of more than three letters
> then a successful match has been returned, so the name is in Wiki
> format.
>
> The thing is, even I do enter a valid Wiki name, such as "WikiName",
> it is refused. So I put in a couple of debugging alert statements
> before the if {} clause, to see what was going on:
>
> alert (wikiName);
> alert (wikiName.length);
>
> The result was:
>
> WikiN
> 1

This is because value.match(reGexp) returns match(es if used with /g
flag) in array form, so your wikiName.length returns the size of the
returned array = 1.
>
> How can this be? The string is correctly matched, and has 5
> characters. So why would its length be reported as 1?
>
> I tried this on Firefox and Explorer, same result both times.
>
> Can anybody solve this mystery?
>
> Thanks for your time,
>
> Dalgetty


 
Reply With Quote
 
maxwells@gmail.com
Guest
Posts: n/a
 
      07-31-2008
Thanks friends, that did it!

I should have realised that match would return an array, but I never
would have guessed that alert() would try to print out an array by
outputting 1!

It works, thanks again

Dalgetty
 
Reply With Quote
 
maxwells@gmail.com
Guest
Posts: n/a
 
      07-31-2008
For the record - the working JavaScript validator for WikiName format:

function checkup(theform){
var form = document.forms[theform];
var okay = true;
if (form.name.value.match(/[A-Z][a-z0-9]*[0-9A-Z]/)) {
wikiName = form.name.value.match(/[A-Z][a-z0-9]*[0-9A-
Z]/).toString();
if (wikiName.length<3) {
alert(form.name.value + ' is not a WikiName - please use one word
with at least two capital letters');
okay = false;
}
}
}
 
Reply With Quote
 
Jorge
Guest
Posts: n/a
 
      07-31-2008
On Jul 31, 3:15*pm, maxwe...@gmail.com wrote:
> For the record - the working JavaScript validator for WikiName format:
>
> function checkup(theform){
> * * * * var form = document.forms[theform];
> * * * * var okay = true;
> * * * * if (form.name.value.match(/[A-Z][a-z0-9]*[0-9A-Z]/)) {
> * * * * * * wikiName = form.name.value.match(/[A-Z][a-z0-9]*[0-9A-
> Z]/).toString();
> * * * * * * * * if (wikiName.length<3) {
> * * * * * * * * * * * * alert(form.name.value + 'is not a WikiName - please use one word
> with at least two capital letters');
> * * * * * * * * * * * * okay = false;
> * * * * * * * * }
> * * * * }
>
>


You could as well use (array)[0], as GArlington pointed out, instead
of (array).toString().

That's probably more correct :

wikiName = form.name.value.match(/[A-Z][a-z0-9]*[0-9A-Z]/)[0];

--Jorge.
 
Reply With Quote
 
Dr J R Stockton
Guest
Posts: n/a
 
      08-01-2008
In comp.lang.javascript message <bb872553-f0ee-4d12-a0ee-203925d5bd62@d7
7g2000hsb.googlegroups.com>, Thu, 31 Jul 2008 06:35:59, Jorge
<> posted:
>That's probably more correct :
>
>wikiName = form.name.value.match(/[A-Z][a-z0-9]*[0-9A-Z]/)[0];



code: wikiName = "xx3d".match(/[A-Z][a-z0-9]*[0-9A-Z]/)[0];

Firefox Error Console :
Error: "xx3d".match(/[A-Z][a-z0-9]*[0-9A-Z]/) has no properties
Source File: file:///C:/HOMEPAGE/js-quick.htm
Line: 286



I suggest building on

M = form.name.value.match(/[A-Z][a-z0-9]*[0-9A-Z]/)
if (M) { wikiName = M[0] ; ... }

But a complete, reliable solution requires a full authoritative
definition of the wikiName format; and it behoves the OP to provide [a
link to] that.

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

--
(c) John Stockton, nr London UK. ?@merlyn.demon.co.uk IE7 FF2 Op9 Sf3
news:comp.lang.javascript FAQ <URL:http://www.jibbering.com/faq/index.html>.
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Wireless Lan on/off switch always litup Terry Wireless Networking 1 08-14-2005 07:15 PM
File copy always interrupted over wireless network =?Utf-8?B?TWF0cml4Y3ViZWQ=?= Wireless Networking 1 02-28-2005 06:00 PM
Why do I always have to repair my network connection after rebooti =?Utf-8?B?Q29uZnVzZWRfQ29uc3VtZXI=?= Wireless Networking 2 11-28-2004 02:24 PM
belkin 54g always works on 11 Mbps hans Wireless Networking 2 11-03-2004 01:17 PM
Trying to create a CSS box that is always is always the width of an image placed inside it (and no wider) Deryck HTML 4 06-22-2004 08:25 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57