473,386 Members | 1,962 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,386 software developers and data experts.

.length is always 1

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
Jul 30 '08 #1
6 2832
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.
Jul 30 '08 #2
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
Jul 30 '08 #3
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
Jul 31 '08 #4
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;
}
}
}
Jul 31 '08 #5
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.
Jul 31 '08 #6
In comp.lang.javascript message <bb872553-f0ee-4d12-a0ee-203925d5bd62@d7
7g2000hsb.googlegroups.com>, Thu, 31 Jul 2008 06:35:59, Jorge
<jo***@jorgechamorro.composted:
>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.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
Aug 1 '08 #7

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

Similar topics

4
by: Paul | last post by:
Hi, (First apologies if this is not the most relevant place to post this but I wasn't sure of where was and I am writing my app in VB.) I'm attempting to parse a binary file for which I have...
7
by: Robert Mark Bram | last post by:
Hi All! How do you get the length of an associative array? var my_cars= new Array() my_cars="Mustang"; my_cars="Station Wagon"; my_cars="SUV"; alert(my_cars.length);
5
by: dam_fool_2003 | last post by:
Hai, I studied that the array size is fixed. But I come across a word called "variable length array". Is it possible to change the array size? So I tried the following: #include<stdio.h>...
4
by: craigkenisston | last post by:
Hi, If I initialize a string like this : string s = new string(' ', 20); s = "Hi!"; Console.WriteLine( "" ); It clearly shows that the declaration of the initial length of the string...
3
by: Sam | last post by:
I want to divide character into 2 section. Example, 200412 divided into 2004 in A block and 12 in B block. Please advise how to use left(string, length) or right(string, length) for above...
10
by: Lyle Fairfield | last post by:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbaac11/html/acfctNZ_HV05186465.asp "If the value of the variant argument is Null, the Nz function returns the number zero or a...
13
by: Martin Herbert Dietze | last post by:
Hi, I need to calculate the physical length of text in a text input. The term "physical" means in this context, that I consider 7bit-Ascii as one-byte-per character. Other characters may be...
0
by: Hannibal111111 | last post by:
I found this code on a site for doing string encryption/decryption. The string will encrypt fine, but I get this error when I try to decrypt. Any idea why? I posted the code below. The error...
2
by: Frank Swarbrick | last post by:
I'm just learning about embedded SQL, so be gentle... My basic question is, if I use a fixed length host variable for a column defined as VARCHAR, will trailing spaces be removed (or not) upon...
5
by: colin | last post by:
some enumerable types like collections use Count but but other enumerable types like arrays use length why the difference? Im not sure what my function is going to be passed yet, some...
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:
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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,...
0
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...

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.