JRS: In article <362b1tF4rt1ogU 1@individual.ne t>, dated Sat, 29 Jan
2005 21:47:28, seen in news:comp.lang. javascript, Dietmar Meier
<usereplytoinst ead@innoline-systemtechnik.d e> posted :[color=blue]
>Steve Darby wrote:
>[color=green]
>> How can I use a regular expression to check that a name in a form
>> field begins with a capital letter?[/color]
>
>var beginsWithCap = /^[A-Z]/.test(Element.n ame);
>
>Since this does not cover "É", "Ä" etc., I would rather use
>
>var firstChar = Element.name.ch arAt(0),
> beginsWithCap = firstChar.toUpp erCase() == firstChar;[/color]
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-Featherstonehau gh, and "proper" names
include such as Pope Gregory XIII.
Dr John Stockton wrote:
[color=blue]
> The OP wrote "a name"; the field might contain something like "dietmar
> meier", in which case two checks would be needed.[/color]
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;
[color=blue]
> And he may need to be more careful than that; surnames include such as
> O'Brien, McCarthy, Chomondeley-Featherstonehau gh, and "proper" names
> include such as Pope Gregory XIII.[/color]
On the other hand, "Piet van der Falk", "Karl von und zum Stein", etc.
should be considered as valid names.
"Mick White" <mwhite13BOGUS@ rochester.rr.co m> wrote in message
news:9bRLd.1741 9$8H2.6437@twis ter.nyroc.rr.co m...[color=blue]
> Dietmar Meier wrote:
>
> [snip][color=green]
>>
>> var s = Element.value,
>> e = 0,
>> valid = s.replace(
>> /\b\S/g,
>> function(a) {
>> e |= a != a.toUpperCase() ;
>> }
>> ), !e;[/color]
>
> 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[/color]
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 <gwagner@agrico reunited.com>
comp.lang.javas cript FAQ - http://jibbering.com/faq
Grant Wagner wrote:
[snip][color=blue]
>
>
> 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-.
>[/color]
Thanks, Grant, I'll have to investigate the "|" operator
..
Mick
Grant Wagner wrote:[color=blue]
> "Mick White" <mwhite13BOGUS@ rochester.rr.co m> wrote in message
> news:9bRLd.1741 9$8H2.6437@twis ter.nyroc.rr.co m...[color=green]
> > Dietmar Meier wrote:
> >
> > [snip][color=darkred]
> >>
> >> var s = Element.value,
> >> e = 0,
> >> valid = s.replace(
> >> /\b\S/g,
> >> function(a) {
> >> e |= a != a.toUpperCase() ;
> >> }
> >> ), !e;[/color]
> >[/color][/color]
<...>
[color=blue]
> He then takes advantage of the fact that the comma operator returns[/color]
the[color=blue]
> value of the last operand to set -valid- to the negated value of -e-.
>[/color]
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 wrote:
[color=blue]
> 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.[/color]
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);
JRS: In article <zGVLd.18717$MX 2.12645@twister .nyroc.rr.com>, dated
Wed, 2 Feb 2005 01:04:31, seen in news:comp.lang. javascript, Mick White
<mwhite13BOGUS@ rochester.rr.co m> posted :[color=blue]
>
>Thanks, Grant, I'll have to investigate the "|" operator[/color]
Pray tell me what should be added to
<URL:http://www.merlyn.demo n.co.uk/js-logic.htm> !
Dr John Stockton wrote:
[color=blue]
> JRS: In article <zGVLd.18717$MX 2.12645@twister .nyroc.rr.com>, dated
> Wed, 2 Feb 2005 01:04:31, seen in news:comp.lang. javascript, Mick White
> <mwhite13BOGUS@ rochester.rr.co m> posted :
>[color=green]
>>Thanks, Grant, I'll have to investigate the "|" operator[/color]
>
>
> Pray tell me what should be added to
> <URL:http://www.merlyn.demo n.co.uk/js-logic.htm> !
>[/color]
Good stuff, John, the truth table looks like a reverse NOR gate to me
(only false if both inputs are false).
Mick White said:[color=blue]
>
>Dr John Stockton wrote:
>[color=green]
>> JRS: In article <zGVLd.18717$MX 2.12645@twister .nyroc.rr.com>, dated
>> Wed, 2 Feb 2005 01:04:31, seen in news:comp.lang. javascript, Mick White
>> <mwhite13BOGUS@ rochester.rr.co m> posted :
>>[color=darkred]
>>>Thanks, Grant, I'll have to investigate the "|" operator[/color]
>>
>>
>> Pray tell me what should be added to
>> <URL:http://www.merlyn.demo n.co.uk/js-logic.htm> !
>>[/color]
>
>
>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[/color]
A "reverse NOR gate" being, of course, an OR gate.
Lee wrote:
[color=blue]
> Mick White said:[/color]
[color=blue][color=green]
>>
>>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[/color]
>
>
> A "reverse NOR gate" being, of course, an OR gate.
>[/color]
Yes of course!!!!
It's been a while since I have worked with SSE.
Mick
Comment