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

Why does one regex and the other not work??

Here is the code to test. This code was generously provided (some
changes by ME) by RobG from this list.

Rob's RE is the one commented out, to have at least 1 to 5 digits
before the decimal and two digits after the decimal, it allows a
negative number.

Same RE, uncommented, is mine, with an escaped $ (dollar sign) so I
could use it for evaluating currency entries.

Why does the uncommented RE not work??

Thank you, Tom

<html>
<head>
<title>Untitled</title>
</head>
<body>
<script type="text/javascript">
function testNum(num, erId){
var er = document.getElementById(erId);
//var re = /^[-]?\d{1,5}(\.\d{1,2})?$/;
var re = /^[\$]?\d{1,5}(\.\d{1,2})?$/;

if (num && !re.test(num) ) {
er.innerHTML = 'Invalid number, required format is [$]nnnn.nn';
} else {
er.innerHTML = '';
}
}
</script>
</head>
<body>
<form action="">
$ <input type="text" name="foo" value="" size="10" maxlength="9"
onkeyup="testNum(this.value, 'msg');"
onkeypress="testNum(this.value, 'msg');" />
<input type="reset" value="clear" />
</form>
<div id="msg"></div>
</body>
</html>

Dec 12 '05 #1
5 1265
Quote http://www.amk.ca/python/howto/regex/ :
"For example, [akm$] will match any of the characters "a", "k", "m", or
"$"; "$" is usually a metacharacter, but inside a character class it's
stripped of its special nature."

Dec 12 '05 #2
Thank you for the reference...I'm trying to match for $ as a currency
symbol and it doesn't work in the above regex as either [$] OR [\$],
but I will try it again as [$]...

Any other help much appreciated...

Thank you, :) tom

Dec 12 '05 #3
Your regular expression expects at least 1 digit, i.e., \d{1,5}

So, a '$', by itself, fails your validation function.

Dec 12 '05 #4
On 12/12/2005 01:03, tlyczko wrote:

[snip]
Why does the uncommented RE not work??
[snip]
var re = /^[\$]?\d{1,5}(\.\d{1,2})?$/;


Care to describe what 'not work' means? What values fail? Apart from the
fact that I wouldn't use a character class (the square brackets), I see
nothing wrong with it, and it behaves as expected here.

[snip]

Mike

--
Michael Winter
Prefix subject with [News] before replying by e-mail.
Dec 12 '05 #5
Ahhh...Thank you Lee.

I have found/tried some regexes that DO allow me to type the $
character without throwing an error, but I do not know how to modify
those other regexes to ensure that the end user types two digits if
they type a decimal...

So it looks like I either give up giving feedback as the person types
or I use onblur or onchange to evaluate the typed data in the text box.

Can't have everything, I suppose. :)

With regard to robocoder's comments, thank you for explaining. I don't
know how to change the first part of the regex to do what you suggest,
so I guess until I have time to learn more about regexes, I have to
fall back on just evaluating without the $ character.

Thank you, everyone, for your help.

Tom

Dec 12 '05 #6

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

Similar topics

16
by: Andrew Baker | last post by:
I am trying to write a function which provides my users with a file filter. The filter used to work just using the VB "Like" comparision, but I can't find the equivilant in C#. I looked at...
1
by: Ada | last post by:
i'm trying to use Regex to match a 4 number group pattern. once a match is found, write it to RichTextBox in red. test data: This is my 4567 test data. 1234this is another line of data. Here's...
7
by: bill tie | last post by:
I'd appreciate it if you could advise. 1. How do I replace "\" (backslash) with anything? 2. Suppose I want to replace (a) every occurrence of characters "a", "b", "c", "d" with "x", (b)...
6
by: tshad | last post by:
Is there a way to use Regex inside of a tag, such as asp:label? I tried something like this but can't make it work: <asp:label id="Phone" text=Regex.Replace('<%# Container.DataItem("Phone")...
8
by: vbmark | last post by:
I'm new to RegEx in vb.net so I'm not sure how to do this. I want to know if a string contains two minus signs "-". If there are two then I want it to return TRUE. I also need to know if the...
6
by: Extremest | last post by:
I have a huge regex setup going on. If I don't do each one by itself instead of all in one it won't work for. Also would like to know if there is a faster way tried to use string.replace with all...
7
by: Extremest | last post by:
I am using this regex. static Regex paranthesis = new Regex("(\\d*/\\d*)", RegexOptions.IgnoreCase); it should find everything between parenthesis that have some numbers onyl then a forward...
3
by: Efi | last post by:
Hello, I am trying to capture empty lines, for example : 1. ABCDEF 2. 3. abcdef I would like to capture line number 2. I am using the following regex - new...
1
by: TtfnJohn | last post by:
I have two small scripts that while on the surface should both work the problem is they don't. Here's the first one: import re testString = 'Thap,fpvi,*!wtyd@*.dip.t-dialin.net:*!ylx@*.dip.t-...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: 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...
0
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.