Connecting Tech Pros Worldwide Forums | Help | Site Map

RegExp and how to use regular expressions

Jeff Sandler
Guest
 
Posts: n/a
#1: Jul 23 '05
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(string), 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>Experimenting with regular expressions and the RegExp
object</TITLE>
<SCRIPT language="javascript">
function evaluateIt(sentString)
{
twoDig = new RegExp("\\d\\d","g");
oneDig = new RegExp("\\d","g");
if(sentString.length == 2)
{
if(twoDig.test(sentString))
{
alert("Two digits. All good.");
return;
}
else
{
alert("Two digits. Not good.");
return
}
} //closes if length==2
if(sentString.length == 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="evaluateIt(thebox.value);return false;">
</FORM>
</BODY>
</HTML>

Michael Winter
Guest
 
Posts: n/a
#2: Jul 23 '05

re: RegExp and how to use regular expressions


On Fri, 03 Sep 2004 14:01:41 -0700, Jeff Sandler
<ffej2ffej@dslextreme.com> wrote:
[color=blue]
> 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(string), but I'm not 100% sure about that,
> either.[/color]

The test method checks that the string matches the regular expression. It
is what's usually used in validation, and much better than isNaN.
[color=blue]
> 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.[/color]

How about:

var digits = /^\d+$/;

if(digits.test(<string>)) {
// <string> only contains numbers
}

The regular expression, digits, ensures that the entire string is made up
of only digits. There must be at least one; there is no maximum limit. If
you want to impose one (though your HTML should do that for you), replace
'+' with {n,m} (including the braces). 'n' is the minimum number, whilst
'm' is the maximum. If you want exactly n digits, use {n}.

Hope that helps,
Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Mick White
Guest
 
Posts: n/a
#3: Jul 23 '05

re: RegExp and how to use regular expressions


Jeff Sandler wrote:
[color=blue]
> 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(string), 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>Experimenting with regular expressions and the RegExp
> object</TITLE>
> <SCRIPT language="javascript">[/color]

function evaluateIt(sentString){
return /^[1-9]?\d$/.test(sentString);
}
Won't accept a two digit number starting with "0", though.
Mick
[color=blue]
> function evaluateIt(sentString)
> {
> twoDig = new RegExp("\\d\\d","g");
> oneDig = new RegExp("\\d","g");
> if(sentString.length == 2)
> {
> if(twoDig.test(sentString))
> {
> alert("Two digits. All good.");
> return;
> }
> else
> {
> alert("Two digits. Not good.");
> return
> }
> } //closes if length==2
> if(sentString.length == 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="evaluateIt(thebox.value);return false;">
> </FORM>
> </BODY>
> </HTML>[/color]
Evertjan.
Guest
 
Posts: n/a
#4: Jul 23 '05

re: RegExp and how to use regular expressions


Mick White wrote on 04 sep 2004 in comp.lang.javascript:
[color=blue]
> function evaluateIt(sentString){
> return /^[1-9]?\d$/.test(sentString);
>}
> Won't accept a two digit number starting with "0", though.
> Mick[/color]

This will even return true: 000007

return /^[1-9]?\d$/.test(+sentString);



--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress,
but let us keep the discussions in the newsgroup)

Michael Winter
Guest
 
Posts: n/a
#5: Jul 23 '05

re: RegExp and how to use regular expressions


On 04 Sep 2004 08:35:02 GMT, Evertjan. <exjxw.hannivoort@interxnl.net>
wrote:

[snip]
[color=blue]
> This will even return true: 000007
>
> return /^[1-9]?\d$/.test(+sentString);[/color]

Of course it will if you type-convert to a number first. I believe (I
can't check at the moment) the conversion will go something like:

'000007' --toInteger (+)--> 7 --toString (test())--> '7'

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Dr John Stockton
Guest
 
Posts: n/a
#6: Jul 23 '05

re: RegExp and how to use regular expressions


JRS: In article <10jhn2v4lgevv8b@corp.supernews.com>, dated Fri, 3 Sep
2004 14:01:41, seen in news:comp.lang.javascript, Jeff Sandler
<ffej2ffej@dslextreme.com> posted :[color=blue]
>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(string), 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.
> ...[/color]

Ugh!

OK = /^\d\d$/.test(S) // two digits
OK = /^\d?\d$/.test(S) // two digits, first optional
OK = /^\d\d?$/.test(S) // two digits, second optional
OK = /^\d{1,2}$/.test(S) // one or two digits

Note use of ^ & $.

You will also, I expect, want to validate those dates.

Read the FAQ on dates; see below; see also my js-valid.htm .

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

re: RegExp and how to use regular expressions


Michael Winter wrote:[color=blue]
> On Fri, 03 Sep 2004 14:01:41 -0700, Jeff Sandler
> <ffej2ffej@dslextreme.com> wrote:
>[color=green]
>> 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(string), but I'm not 100% sure about
>> that, either.[/color]
>
>
> The test method checks that the string matches the regular expression.
> It is what's usually used in validation, and much better than isNaN.
>[color=green]
>> 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.[/color]
>
>
> How about:
>
> var digits = /^\d+$/;
>
> if(digits.test(<string>)) {
> // <string> only contains numbers
> }
>
> The regular expression, digits, ensures that the entire string is made
> up of only digits. There must be at least one; there is no maximum
> limit. If you want to impose one (though your HTML should do that for
> you), replace '+' with {n,m} (including the braces). 'n' is the minimum
> number, whilst 'm' is the maximum. If you want exactly n digits, use {n}.
>
> Hope that helps,
> Mike
>[/color]
Mike,
Your input has helped tremendously. I have used your regular expression
many times in my code already. Now I have a new variation on my
original question.
I'm still doing the same script, only now, the user is allowed to enter
a real (floating point, float, whatever) number. Thus, the var digits
must now accept a decimal point at pretty much any position in addition
to digitis. Can this be done easily? Could it be something as simple
as changing the statement to read
var digits = /.,^d+$/
I should also point out that the user is allowed 7 characters in this
case unlike the last case where he was only allowed 2 characters.
Andrew Thompson
Guest
 
Posts: n/a
#8: Jul 23 '05

re: RegExp and how to use regular expressions


On Fri, 10 Sep 2004 09:11:38 -0700, Jeff Sandler wrote:
[color=blue]
> Can this be done easily? Could it be something as simple
> as changing the statement to read
> var digits = /.,^d+$/[/color]

What happened when you tried that?

--
Andrew Thompson
http://www.PhySci.org/ Open-source software suite
http://www.PhySci.org/codes/ Web & IT Help
http://www.1point1C.org/ Science & Technology
Michael Winter
Guest
 
Posts: n/a
#9: Jul 23 '05

re: RegExp and how to use regular expressions


On Fri, 10 Sep 2004 16:27:14 GMT, Andrew Thompson <SeeMySites@www.invalid>
wrote:
[color=blue]
> On Fri, 10 Sep 2004 09:11:38 -0700, Jeff Sandler wrote:
>[color=green]
>> Can this be done easily? Could it be something as simple
>> as changing the statement to read
>> var digits = /.,^d+$/[/color]
>
> What happened when you tried that?[/color]

It might error. The carat (^) marks the beginning of the input. Characters
placed before it would be ignored, and might be regarded as a malformed
pattern.

Jeff, do you have any specific constraints, other than the maximum length?
For example, can the input accept an integer, or must it contain a period?
Do you require at least one number before and after the period?

This will accept, at a minimum, '0.0'. That is, it must contain a period.

function isReal(s) {
return /^\d+\.\d+$/.test(s) && 8 > s.length;
}

You can change the regular expression literal to

/^\d+(\.\d+)?$/

if an integer is also acceptable.

Hope that helps,
Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jeff Sandler
Guest
 
Posts: n/a
#10: Jul 23 '05

re: RegExp and how to use regular expressions


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.



*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Dr John Stockton
Guest
 
Posts: n/a
#11: Jul 23 '05

re: RegExp and how to use regular expressions


JRS: In article <4141ec04$0$26091$c397aba@news.newsgroups.ws>, dated
Fri, 10 Sep 2004 18:01:40, seen in news:comp.lang.javascript, Jeff
Sandler <ffej2ffej@dslextreme.com> posted :[color=blue]
>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.[/color]

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.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Closed Thread