473,766 Members | 2,060 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Adding hyphens and underscore to my JS expression test

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.

Rgds

Robbie
Dec 20 '05 #1
38 4539
On 20/12/2005 15:35, Astra wrote:

[snip]
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.


Yes, that is what should happen. However, why not avoid confusion by
placing the dash last in the character class ([...9_-])?

[snip]

Mike

--
Michael Winter
Prefix subject with [News] before replying by e-mail.
Dec 20 '05 #2
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.

Grey

--
The technical axiom that nothing is impossible sinisterly implies the
pitfall corollary that nothing is ridiculous.
- http://www.greywyvern.com/orca#sear - Orca Search - PHP/MySQL site
search engine
Dec 20 '05 #3
On 20/12/2005 15:58, GreyWyvern wrote:

[snip]
The hyphen character is *always* a special character within square
brackets in regular expressions.


Not at all. It's a special character when between a pair of characters
in which case it forms a range, but not in the OP's case (the 9 is
already part of a pair) and not at the very beginning or end of a class
(which is where I suggested it should go).

[snip]

Mike

--
Michael Winter
Prefix subject with [News] before replying by e-mail.
Dec 20 '05 #4
Many thanks guys.

Much appreciated.

Rgds Robbie

"Michael Winter" <m.******@bluey onder.co.uk> wrote in message
news:K3******** **********@text .news.blueyonde r.co.uk...
On 20/12/2005 15:35, Astra wrote:

[snip]
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.


Yes, that is what should happen. However, why not avoid confusion by
placing the dash last in the character class ([...9_-])?

[snip]

Mike

--
Michael Winter
Prefix subject with [News] before replying by e-mail.
Dec 20 '05 #5

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

Dec 20 '05 #6
Michael Winter wrote:
On 20/12/2005 15:35, Astra wrote:
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.


Yes, that is what should happen. However, why not avoid confusion by
placing the dash last in the character class ([...9_-])?


Why not /^[\w-]*$/?
F'up2 cljs

PointedEars
Dec 20 '05 #7
Norman L. DeForest wrote:
Quoting from one JavaScript reference of mine,

[...]
: <HTML>
: <SCRIPT LANGUAGE = "JavaScript1.2" > ^^^^^^^^[1] ^^^^^^^^^^^^^[2] :
: re = /\(?\d{3}\)?([-\/\.])\d{3}\1\d{4}/; ^^[3] ^^[4] [snip]


At the latest here, you should burn your "Reference" .

[1] That is not Valid HTML at all: <URL:http://validator.w3.or g/>

You are correct in what you quoted from the ECMAScript specification.
However, ECMAScript 3 is not the relevant Edition for JavaScript 1.2,
that is ECMAScript 1, which did not define Regular Expressions before
Edition 3, that were JavaScript/JScript extensions at the time:

<URL:http://research.nihons oft.org/javascript/jsref/corea3.htm#1158 210>
<URL:http://msdn.microsoft. com/library/en-us/jscript7/html/jsobjregexpress ion.asp>

[2]
JavaScript 1.2 does not conform to ECMAScript 2 because when
the latter was still written, the former was already released:

<URL:http://developer.mozil la.org/en/docs/Core_JavaScript _1.5_Guide:Java Script_Overview #Relationship_b etween_JavaScri pt_and_ECMA_Ver sions>

With rare exceptions, you should never try to force the script engine
into JavaScript 1.2 compliant mode with "language='Java Script1.2'".

[3] Undeclared global variable.
[4] The literal dot does not need to be escaped in ranges.
F'up2 cljs

PointedEars
Dec 20 '05 #8
And lo, Michael Winter didst speak in
alt.www.webmaster,comp.lang.javascript:
On 20/12/2005 15:58, GreyWyvern wrote:

[snip]
The hyphen character is *always* a special character within square
brackets in regular expressions.


Not at all. It's a special character when between a pair of characters
in which case it forms a range, but not in the OP's case (the 9 is
already part of a pair) and not at the very beginning or end of a class
(which is where I suggested it should go).


My apologies. I meant to say: one should *assume* the hyphen character is
always a special character within square brackets. Shortcuts like using
it as the first or last character within the brackets and not escaping it
are so easy to break down, especially if the regular expression is
dynamically created.

Grey

--
The technical axiom that nothing is impossible sinisterly implies the
pitfall corollary that nothing is ridiculous.
- http://www.greywyvern.com/orca#sear - Orca Search - PHP/MySQL site
search engine
Dec 20 '05 #9
On 20/12/2005 18:15, Thomas 'PointedEars' Lahn wrote:

[snip]
Why not /^[\w-]*$/?
:-D

Yes, that will do, too. Unless you're trying to avoid a bug ('feature'?)
in Mozilla which includes other characters from the Unicode repertoire.
F'up2 cljs


Last time I did that, someone in a.w.webmaster complained vehemently, so
I decided to avoid the hassle in my previous post.

Mike

--
Michael Winter
Prefix subject with [News] before replying by e-mail.
Dec 20 '05 #10

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

Similar topics

1
1515
by: Michelle | last post by:
I have been trying to work out a regular expression to test for ONLY numbers, no hyphens so that I can accept only hyphen-less phone numbers...I have had no luck Any ideas out there... TIA, Miki
5
4974
by: richard_thomas | last post by:
Hi, I'm having trouble running the following query: select * from message where text_body like ' ----------%' ie, five spaces followed by at least ten hyphens. The query doesn't complete, so eventually I cancel it. If I remove the hyphens from the query ("... like ' %'") then it runs fine (though it doesn't find the correct data).
18
20528
by: Gustaf Liljegren | last post by:
IE does, and I can't remember this used to be a problem in Netscape. I guess someone in the Mozilla team just came up with a Smart Idea about the True Semantics of the Hyphen Minus character. :-( How do I make IE and Firefox agree to make hyphens trigger a linebreak? Gustaf
2
6055
by: Herbert Liechti | last post by:
Hello I like to grant some permissions to the user www-data (default user for apache under debian) GRANT SELECT, UPDATE, INSERT, DELETE ON customer TO herbie, www-data; The statements throws the following error: ERROR: parser: parse error at or near "-" at character 64
46
2740
by: James Harris | last post by:
Before I embark on a new long-term project I'd appreciate your advice on how to split up long names. I would like to keep the standards for command names the same as that for variable names. Looking at the examples below, which ones seem better? Straight names echoclient lastcharoffset helloworld Internal underscores
1
10194
by: Guadala Harry | last post by:
The following function, SplitTheString(), splits a string based on a flag that is like this: {Flag1:whateverHere} Note that the "whateverHere" part can be any character - and at least one character. I simply want to extend this RegEx to do the same job - but to do it based on an additional possible flag: {Flag2:whateverHere}
5
2312
by: ibiza | last post by:
Hi all, I have a question which I have no ideal of the answer...I am currently working on a web application and at some time, I have a string representing a short text. This could be a simple example : "This is my list :\n\r-list item 1\n\r-list item 2\n\r-list item 3\n\r\n\rThis was a great list." Let's say this outputs like that :
16
3385
by: Jim Langston | last post by:
I know that functions starting with an underscore, or two underscores, are reserved by the compiler/c++ and should not be used by the user and may cause undefined behavior. My question is, how likely is it to actually cause undefined behavior? The reason I'm asking is I'm using a game engine where the sockets code is not working correctly on my computer, but seems to work correctly on everyone elses. I am not compiling the dll myself,...
4
4107
by: almurph | last post by:
Hi, Wondering if you can help me here. Given a string length "m" how do you insert 3 hyphens in differing positions such that the following conditions are met: a. all positions are covered b. no pattern is repeated.
0
9568
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9404
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10168
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10008
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8833
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6651
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5423
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3929
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3532
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.