473,804 Members | 3,953 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

RegExp and how to use regular expressions

I have a page that accepts input from many textboxes. Many of the
textboxes are intended to accept dates and times, thus, I expect only
digits to be entered. I originally tested using parseInt and isNaN, but
I'm not even sure that the results are as perfect as I need. I am
expecting to use RegExp.test(str ing), but I'm not 100% sure about that,
either.
Here is a test program with a textbox that has a maxlength of 2
characters. The function evaluateIt() measures the string for length.
If 2 characters, it tests against the regular expression \d\d. If 1
character, it tests against \d. This seems to be effective, but isn't
there a way to check either of these cases with one test? I tried
if(test(oneDig) || test(twoDig)), but this will allow a string of 1
digit and one non-digit to pass.
Here is the test program as I wrote it.
<HTML>
<HEAD><TITLE>Ex perimenting with regular expressions and the RegExp
object</TITLE>
<SCRIPT language="javas cript">
function evaluateIt(sent String)
{
twoDig = new RegExp("\\d\\d" ,"g");
oneDig = new RegExp("\\d","g ");
if(sentString.l ength == 2)
{
if(twoDig.test( sentString))
{
alert("Two digits. All good.");
return;
}
else
{
alert("Two digits. Not good.");
return
}
} //closes if length==2
if(sentString.l ength == 1)
{
if(oneDig.test( sentString))
{
alert("One digit. All good.");
return;
}
else
{
alert("One digit. Not good.");
return;
}
} //closes if length==1
alert("Neither 1 nor 2 digits. Not good.");
return;
} //closes evaluateIt function
</SCRIPT>
</HEAD>
<BODY>
<FORM action="nothing .htm" method=POST>
<BR><BR>
Type something in the box to check against a regular expression
<INPUT type=text size=2 maxlength=2 name="thebox">< BR><BR>
<INPUT type=submit value="Do It"
onclick="evalua teIt(thebox.val ue);return false;">
</FORM>
</BODY>
</HTML>
Jul 23 '05
10 2221
JRS: In article <41************ **********@news .newsgroups.ws> , dated
Fri, 10 Sep 2004 18:01:40, seen in news:comp.lang. javascript, Jeff
Sandler <ff*******@dsle xtreme.com> posted :
The constraints are exactly this:
The user is entering a dollar amount and is instructed to not enter
dollar signs nor commas. He may, however, enter a single period to
represent a decimal point. Thus, he must enter 1 thru 7 characters and
all must be either a digit or period. There can be either 0 or 1
periods and no more. A period at the last position is sort of
senseless, but acceptable still.


For a sum of money, one should accept nothing other than either digits,
or digits point digit digit (in countries where the smallest unit is a
cent-equivalent). Anything else is too likely to be an error.

It may be wise to limit the total sum; for a small local firm, a sum of
over, say, a million is generally unreasonable.

It may be wise to insist on the cents, especially if round hundreds are
not common.

The form of constraint you describe is not entirely reasonable; the task
seems to be either course-work or emanating from a PHM.

OK = /^\d+$/.test(X.value) // units
OK = /^\d+\.\d\d$/.test(X.value) // units . cents
OK = /^\d+(\.\d\d)?$/.test(X.value) // cents optional
OK = /^\d{1,6}(\.\d\d )?$/.test(X.value) // under a million

The leading \d+ can be replaced by [1-9]\d* if sums less than a major
unit are ruled out.

Don't use parseInt with a single parameter for money.

Decimal points not surrounded by digits should be accepted or generated
only with great reluctance.

--
© John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.c om/faq/> JL/RC: FAQ of news:comp.lang. javascript
<URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 23 '05 #11

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

Similar topics

4
5495
by: | last post by:
Hi, I'm fairly new to regular expressions, and this may be a rather dumb question, but so far I haven't found the answer in any tutorial or reference yet... If I have f.i. the string "The {{{{power of {{{{regular expressions}}}} comes from}}}} the ability to include alternatives and repetitions in the pattern." from which I want to extract chunks starting with "{{{{" and ending with "}}}}".
2
2369
by: Ron Brennan | last post by:
Good morning. I would like to use one or more RegExp to validate country names as having the first and last words beginning with an uppercase letter, intermediate words beginning with either uppercase or lowercase, and all other characters being lowercase characters. The difficult part is that I want to obtain save the string positions of the invalid characters in an array, rather than just obtain a true or false value. Can anybody...
10
39359
by: Anand Pillai | last post by:
To search a word in a group of words, say a paragraph or a web page, would a string search or a regexp search be faster? The string search would of course be, if str.find(substr) != -1: domything() And the regexp search assuming no case restriction would be,
5
1836
by: Dr John Stockton | last post by:
ISTM that RegExps deserve a FAQ entry, with links to more detailed sources. An important question, probably not treated by many otherwise worthwhile sources, must be on feature detection of the newer RegExp facilities - for example, greedy/non-greedy. The answer may be that it is not possible to do so in a safe manner; that one can do no better than something like
8
2035
by: Dmitry Korolyov | last post by:
ASP.NET app using c# and framework version 1.1.4322.573 on a IIS 6.0 web server. A single-line asp:textbox control and regexp validator attached to it. ^\d+$ expression does match an empty string (when you don't enter any values) - this is wrong d+ expression does not match, for example "g24" string - this is also wrong www.regexplib.com test validator works fine for both cases, i.e. it is reporting "not match" for the...
7
1912
by: arno | last post by:
Hi, I want to search a substring within a string : fonction (str, substr) { if (str.search(substr) != -1) { // do something } }
9
6904
by: =?ISO-8859-1?Q?BJ=F6rn_Lindqvist?= | last post by:
With regexps you can search for strings matching it. For example, given the regexp: "foobar\d\d\d". "foobar123" would match. I want to do the reverse, from a regexp generate all strings that could match it. The regexp: "{3}\d{3}" should generate the strings "AAA000", "AAA001", "AAA002" ... "AAB000", "AAB001" ... "ZZZ999". Is this possible to do? Obviously, for some regexps the set of matches is unbounded (a list of everything that...
27
3250
by: SQL Learner | last post by:
Hi all, I have an Access db with two large tables - 3,100,000 (tblA) and 7,000 (tblB) records. I created a select query using Inner Join by partial matching two fields (X from tblA and Y from tblB). The size of the db is about 200MBs. Now my issue is, the query has been running for over 3 hours already - I have no idea when it will end. I am using Access 2003. Are there ways to improve the speed performance? (Also, would the...
5
1075
by: Johny | last post by:
I have the following text <title>Goods Item 146 (174459989) - OurWebSite</title> from which I need to extract `Goods Item 146 ' Can anyone help with regexp? Thank you for help L.
0
9582
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
10335
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...
1
10323
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9157
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...
1
7621
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6854
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
5652
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3821
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2993
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.