By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,465 Members | 1,172 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,465 IT Pros & Developers. It's quick & easy.

Names in forms

P: n/a
Hi there,

How can I use a regular expression to check that a name in a form field
begins with a capital letter?
Jul 23 '05 #1
Share this Question
Share on Google+
12 Replies


P: n/a
Steve Darby wrote:
How can I use a regular expression to check that a name in a form
field begins with a capital letter?


var beginsWithCap = /^[A-Z]/.test(Element.name);

Since this does not cover "", "" etc., I would rather use

var firstChar = Element.name.charAt(0),
beginsWithCap = firstChar.toUpperCase() == firstChar;

ciao, dhgm

Jul 23 '05 #2

P: n/a
JRS: In article <36*************@individual.net>, dated Sat, 29 Jan
2005 21:47:28, seen in news:comp.lang.javascript, Dietmar Meier
<us***************@innoline-systemtechnik.de> posted :
Steve Darby wrote:
How can I use a regular expression to check that a name in a form
field begins with a capital letter?


var beginsWithCap = /^[A-Z]/.test(Element.name);

Since this does not cover "", "" etc., I would rather use

var firstChar = Element.name.charAt(0),
beginsWithCap = firstChar.toUpperCase() == firstChar;


The OP wrote "a name"; the field might contain something like "dietmar
meier", in which case two checks would be needed.

And he may need to be more careful than that; surnames include such as
O'Brien, McCarthy, Chomondeley-Featherstonehaugh, and "proper" names
include such as Pope Gregory XIII.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 MIME.
Web <URL:http://www.merlyn.demon.co.uk/> - w. FAQish topics, links, acronyms
PAS EXE etc : <URL:http://www.merlyn.demon.co.uk/programs/> - see 00index.htm
Dates - miscdate.htm moredate.htm js-dates.htm pas-time.htm critdate.htm etc.
Jul 23 '05 #3

P: n/a
Dr John Stockton wrote:
The OP wrote "a name"; the field might contain something like "dietmar
meier", in which case two checks would be needed.
If that's the case, I would suggest

var s = Element.value,
e = 0,
valid = s.replace(
/\b\S/g,
function(a) {
e |= a != a.toUpperCase();
}
), !e;
And he may need to be more careful than that; surnames include such as
O'Brien, McCarthy, Chomondeley-Featherstonehaugh, and "proper" names
include such as Pope Gregory XIII.


On the other hand, "Piet van der Falk", "Karl von und zum Stein", etc.
should be considered as valid names.

ciao, dhgm
Jul 23 '05 #4

P: n/a
Dietmar Meier wrote:

[snip]

var s = Element.value,
e = 0,
valid = s.replace(
/\b\S/g,
function(a) {
e |= a != a.toUpperCase();
}
), !e;


e |= a != a.toUpperCase();

I've stared at this long enough, Dietmar, can you explain what's
happening in this line of code? Specifically,"e |="

Mick
Jul 23 '05 #5

P: n/a
"Mick White" <mw***********@rochester.rr.com> wrote in message
news:9b******************@twister.nyroc.rr.com...
Dietmar Meier wrote:

[snip]

var s = Element.value,
e = 0,
valid = s.replace(
/\b\S/g,
function(a) {
e |= a != a.toUpperCase();
}
), !e;


e |= a != a.toUpperCase();

I've stared at this long enough, Dietmar, can you explain what's
happening in this line of code? Specifically,"e |="

Mick


e = e | (a != a.toUpperCase());

So -e- gets set to 1 if -e- is true (or non-null, or a non-empty string,
or a non-zero number, or not undefined) or -a- is not equal to -a-
converted to uppercase.

He then takes advantage of the fact that the comma operator returns the
value of the last operand to set -valid- to the negated value of -e-.

--
Grant Wagner <gw*****@agricoreunited.com>
comp.lang.javascript FAQ - http://jibbering.com/faq
Jul 23 '05 #6

P: n/a
Grant Wagner wrote:
[snip]


e = e | (a != a.toUpperCase());

So -e- gets set to 1 if -e- is true (or non-null, or a non-empty string,
or a non-zero number, or not undefined) or -a- is not equal to -a-
converted to uppercase.

He then takes advantage of the fact that the comma operator returns the
value of the last operand to set -valid- to the negated value of -e-.


Thanks, Grant, I'll have to investigate the "|" operator
..
Mick
Jul 23 '05 #7

P: n/a
rh
Grant Wagner wrote:
"Mick White" <mw***********@rochester.rr.com> wrote in message
news:9b******************@twister.nyroc.rr.com...
Dietmar Meier wrote:

[snip]

var s = Element.value,
e = 0,
valid = s.replace(
/\b\S/g,
function(a) {
e |= a != a.toUpperCase();
}
), !e;


<...>
He then takes advantage of the fact that the comma operator returns the value of the last operand to set -valid- to the negated value of -e-.


Not as given, though -- that will produce a syntax error in the "var"
statement, as "!e" is not an identifier. Bracketing the expression
"(...)" assigned to "valid" should give the intended effect that you
describe.
../rh

Jul 23 '05 #8

P: n/a
rh wrote:
Not as given, though -- that will produce a syntax error in the "var"
statement, as "!e" is not an identifier. Bracketing the expression
"(...)" assigned to "valid" should give the intended effect that you
describe.


Yes, absolutely correct. Disadvantage of copy&pray: Took it from
another piece of code where I had declared "valid" some lines
before and then simply added the comma before "valid = ...".

Corrected:

var s = Element.value,
e = 0,
valid = (s.replace(
/\b\S/g,
function(a) {
e |= a != a.toUpperCase();
}
), !e);
ciao, dhgm
Jul 23 '05 #9

P: n/a
JRS: In article <zG*******************@twister.nyroc.rr.com>, dated
Wed, 2 Feb 2005 01:04:31, seen in news:comp.lang.javascript, Mick White
<mw***********@rochester.rr.com> posted :

Thanks, Grant, I'll have to investigate the "|" operator


Pray tell me what should be added to
<URL:http://www.merlyn.demon.co.uk/js-logic.htm> !
--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<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.
Jul 23 '05 #10

P: n/a
Dr John Stockton wrote:
JRS: In article <zG*******************@twister.nyroc.rr.com>, dated
Wed, 2 Feb 2005 01:04:31, seen in news:comp.lang.javascript, Mick White
<mw***********@rochester.rr.com> posted :
Thanks, Grant, I'll have to investigate the "|" operator

Pray tell me what should be added to
<URL:http://www.merlyn.demon.co.uk/js-logic.htm> !

Good stuff, John, the truth table looks like a reverse NOR gate to me
(only false if both inputs are false).

1|0 : 1
1|1 : 1
0|1 : 1
0|0 : 0
Thanks.
Mick

Jul 23 '05 #11

P: n/a
Lee
Mick White said:

Dr John Stockton wrote:
JRS: In article <zG*******************@twister.nyroc.rr.com>, dated
Wed, 2 Feb 2005 01:04:31, seen in news:comp.lang.javascript, Mick White
<mw***********@rochester.rr.com> posted :
Thanks, Grant, I'll have to investigate the "|" operator

Pray tell me what should be added to
<URL:http://www.merlyn.demon.co.uk/js-logic.htm> !

Good stuff, John, the truth table looks like a reverse NOR gate to me
(only false if both inputs are false).

1|0 : 1
1|1 : 1
0|1 : 1
0|0 : 0


A "reverse NOR gate" being, of course, an OR gate.

Jul 23 '05 #12

P: n/a
Lee wrote:
Mick White said:


Good stuff, John, the truth table looks like a reverse NOR gate to me
(only false if both inputs are false).

1|0 : 1
1|1 : 1
0|1 : 1
0|0 : 0

A "reverse NOR gate" being, of course, an OR gate.


Yes of course!!!!
It's been a while since I have worked with SSE.
Mick
Jul 23 '05 #13

This discussion thread is closed

Replies have been disabled for this discussion.