.length is always 1

M

maxwells

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
 
J

Jorge

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.
 
G

GArlington

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.
 
M

maxwells

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
 
M

maxwells

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;
}
}
}
 
J

Jorge

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.
 
D

Dr J R Stockton

In comp.lang.javascript message <bb872553-f0ee-4d12-a0ee-203925d5bd62@d7
7g2000hsb.googlegroups.com>, Thu, 31 Jul 2008 06:35:59, Jorge
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.
 

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

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
DewittMill
Top