On Tue, 20 Dec 2005, GreyWyvern wrote:
And lo, Astra didst speak in alt.www.webmaster,comp.lang.javascript:
Hi All
Could somebody please confirm that if I change my JS expression test
from:
if (!(/^[a-zA-Z0-9]*$/.test(document. form1.fred.valu e)))
to
if (!(/^[a-zA-Z0-9-_]*$/.test(document. form1.fred.valu e)))
(in other words I've added the hyphen and underscore after the 9)
then I'm allowing the hyphen and underscore as valid chars like a-z 0-9,
but nothing else.
It appears to work, but I don't know if I've screwed up the whole test
rather just the chars I want to add.
The hyphen character is *always* a special character within square
brackets in regular expressions. You'll need to escape it with a
backslash.
if (!(/^[a-zA-Z0-9\-_]*$/.test(document. form1.fred.valu e)))
It is important to note that outside of square brackets, the hyphen has
*no* special meaning and can be left unescaped.
I thought that the hyphen is specified as a literal in a character range
by putting it first.
if (!(/^[-a-zA-Z0-9_]*$/.test(document. form1.fred.valu e)))
^^^^^^^^^^^^^
'-' or ( 'a' to 'z') or ( 'A' to 'Z') or ( '0' to '9') or '_'
'/' and '.' have to be escaped, though.
Quoting from one JavaScript reference of mine,
[snip]
: Using Special Characters to Verify Input
:
: In the following example, a user enters a phone number. When the user
: presses Enter, the script checks the validity of the number. If the number
: is valid (matches the character sequence specified by the regular
: expression), the script posts a window thanking the user and confirming
: the number. If the number is invalid, the script posts a window informing
: the user that the phone number is not valid.
:
: The regular expression looks for zero or one open parenthesis \(?,
: followed by three digits \d{3}, followed by zero or one close parenthesis
: \)?, followed by one dash, forward slash, or decimal point and when found,
: remember the character ([-\/\.]), followed by three digits \d{3}, followed
: by the remembered match of a dash, forward slash, or decimal point \1,
: followed by four digits \d{4}.
:
: The Change event activated when the user presses Enter sets the value of
: RegExp.input.
:
: <HTML>
: <SCRIPT LANGUAGE = "JavaScript1.2" >
:
: re = /\(?\d{3}\)?([-\/\.])\d{3}\1\d{4}/;
[snip]
The ECMA-262 standard says:
[pasting and reformatting from the PDF]
[snip]
: 15.10.2.16 NonemptyClassRa ngesNoDash
[snip]
: Informative comments: ClassRanges can expand into single ClassAtoms and/or
: ranges of two ClassAtoms separated by dashes. In the latter case the
: ClassRanges includes all characters between the first ClassAtom and the
: second ClassAtom, inclusive; an error occurs if either ClassAtom does not
: represent a single character (for example, if one is \w) or if the first
: ClassAtom's code point value is greater than the second ClassAtom's code
: point value. Even if the pattern ignores case, the case of the two ends of
: a range is significant in determining which characters belong to the
: range. Thus, for example, the pattern /[E-F]/i matches only the
: letters E, F, e,andf, while the pattern /[E-f]/i matches all upper and
: lower-case ASCII letters as well as the symbols [, \, ], ^, _,and`.
: A - character can be treated literally or it can denote a range. It is
^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^ ^^^^^
: treated literally if it is the first or last character of ClassRanges, the
^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^
: beginning or end limit of a range specification, or immediately follows a
^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^
: range specification.
^^^^^^^^^^^^^^^ ^^^^^
[snip]
--
Norman De Forest
http://www.chebucto.ns.ca/~af380/Profile.html af***@chebucto. ns.ca [=||=] (At the Sign of the Flashing Cursor)
"Oh how I miss the days when it was easier to catch gonorhea than a
computer virus." -- Big Will in alt.comp.virus, March 9, 2005