By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,639 Members | 2,340 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,639 IT Pros & Developers. It's quick & easy.

beginner: regex how to check that value is only numbers

P: n/a
hello,

i must check if the value of a text field is a number (they have to fill in
a number between 7 and 10 digits),
so: 1234567 is allowed
and 123456789999 is not allowed;

also, there are no other characteres allowed:
wrong: 12345ABV
wrong: 12#$as1123

only 1234567 or 12345678 or 123456789 or 1234567899 is allowed

i have tried something like

------------------ source code --------------------------------
patroon = /[0-9]+/
if(!patroon.test(invoer)){
alert("some characters are not allowed: " + invoer)
document.getElementById(veld).focus()
return false
}
-----------------------------------------------------------------

when i fill in: qqqqqqq it works, but when i fil in 12345wert it accepts it.

thanks
Jan 24 '06 #1
Share this Question
Share on Google+
10 Replies


P: n/a
nescio said the following on 1/24/2006 4:35 AM:
hello,

i must check if the value of a text field is a number (they have to fill in
a number between 7 and 10 digits),
so: 1234567 is allowed
and 123456789999 is not allowed;


function checkNumber(numToCheck){
if(parseInt(numToCheck,10)==numToCheck&&(numToChec k.length>7)&&numToCheck.length<10)
{alert('valid')}
else
{alert('invalid')}
}

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Jan 24 '06 #2

P: n/a
Randy Webb wrote on 24 jan 2006 in comp.lang.javascript:
nescio said the following on 1/24/2006 4:35 AM:
hello,

i must check if the value of a text field is a number (they have to
fill in a number between 7 and 10 digits),
so: 1234567 is allowed
and 123456789999 is not allowed;


function checkNumber(numToCheck){
if(parseInt(numToCheck,10)==numToCheck&&(numToChec k.length>7)&&numToChe
ck.length<10) {alert('valid')}
else
{alert('invalid')}
}


numToCheck = '1234567';

result = /^\d{7,10}$/.test(numToCheck);

alert( (result)?'valid':'invalid' )
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jan 24 '06 #3

P: n/a
"nescio" <ne****@nescio.nl> wrote:

------------------ source code --------------------------------
patroon = /[0-9]+/


This pattern checks for a string of digits anywhere in the string that
you're checking. If you want the *entire* string to be digits, then
you need to use the beginning of line and end of line characters:

patroon = /^[0-9]$/

That checks for beginning-of-line followed by a sting of digits
followed by end-of-line. No room there for anything except digits.
Evertjan's post shows how to add a length specifier to that so that
the string is limited to at least seven and at most 10 digits, as well
as using the \d abbreviation for [0-9].

--
Tim Slattery
Sl********@bls.gov
Jan 24 '06 #4

P: n/a
Slight correction

patroon = /^[0-9]+$/

You'll need the + or it'll only match single digits.

Jan 24 '06 #5

P: n/a
wrote on 24 jan 2006 in comp.lang.javascript:
Slight correction

patroon = /^[0-9]+$/

You'll need the + or it'll only match single digits.


Please quote what you are replying to.

If you want to post a followup via groups.google.com, don't use the
"Reply" link at the bottom of the article. Click on "show options" at the
top of the article, then click on the "Reply" at the bottom of the article
headers.

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jan 24 '06 #6

P: n/a
Tim Slattery wrote:
"nescio" <ne****@nescio.nl> wrote:
patroon = /[0-9]+/


This pattern checks for a string of digits anywhere in the string that
you're checking. If you want the *entire* string to be digits, then
you need to use the beginning of line and end of line characters:

patroon = /^[0-9]$/

That checks for beginning-of-line followed by a sting of digits
followed by end-of-line. [...]


That is actually beginning-of-input and end-of-input. Your definition
applies to

/^[0-9]$/m
PointedEars
Jan 24 '06 #7

P: n/a
Tim Slattery wrote:
"nescio" <ne****@nescio.nl> wrote:
patroon = /[0-9]+/


This pattern checks for a string of digits anywhere in the string that
you're checking. If you want the *entire* string to be digits, then
you need to use the beginning of line and end of line characters:

patroon = /^[0-9]$/

That checks for beginning-of-line followed by a sting of digits
followed by end-of-line. [...]


That is actually beginning-of-input and end-of-input, where
input is but one decimal digit. Your definition applies to

/^[0-9]+$/m
PointedEars
Jan 24 '06 #8

P: n/a
nescio wrote:
hello,

i must check if the value of a text field is a number (they have to fill in
a number between 7 and 10 digits),
so: 1234567 is allowed
and 123456789999 is not allowed;

also, there are no other characteres allowed:
wrong: 12345ABV
wrong: 12#$as1123

only 1234567 or 12345678 or 123456789 or 1234567899 is allowed

function checkNum(num)
{
return /^\d{7,10}$/.test(num);
}

[...]

--
Rob
Jan 24 '06 #9

P: n/a
JRS: In article <Iq********************@comcast.com>, dated Tue, 24 Jan
2006 05:28:01 remote, seen in news:comp.lang.javascript, Randy Webb
<Hi************@aol.com> posted :
nescio said the following on 1/24/2006 4:35 AM:

i must check if the value of a text field is a number (they have to fill in
a number between 7 and 10 digits),
so: 1234567 is allowed
and 123456789999 is not allowed;


function checkNumber(numToCheck){
if(parseInt(numToCheck,10)==numToCheck&&(numToChe ck.length>7)&&numToCheck.length<10)
{alert('valid')}
else
{alert('invalid')}
}


No. It accepts "123456e0" and "0x000000" and "+1234567"; the first
is a value outside the possibly-intended range, and the user asks for
all-digit input.

Also, you've taken "between" to mean "between", which is not what the
ignorant generally intend.

The OP may well not want "0000000" or "0004567" so the RegExp way should
probably be like

OK = /^[1-9]\d{6,9}$/.test(Meadow)
--
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.
Jan 25 '06 #10

P: n/a
Dr John Stockton said the following on 1/25/2006 12:51 PM:
JRS: In article <Iq********************@comcast.com>, dated Tue, 24 Jan
2006 05:28:01 remote, seen in news:comp.lang.javascript, Randy Webb
<Hi************@aol.com> posted :
nescio said the following on 1/24/2006 4:35 AM:
i must check if the value of a text field is a number (they have to fill in
a number between 7 and 10 digits),
so: 1234567 is allowed
and 123456789999 is not allowed;

function checkNumber(numToCheck){
if(parseInt(numToCheck,10)==numToCheck&&(numToChec k.length>7)&&numToCheck.length<10)
{alert('valid')}
else
{alert('invalid')}
}


No. It accepts "123456e0" and "0x000000" and "+1234567"; the first
is a value outside the possibly-intended range, and the user asks for
all-digit input.


So it does. And that is what was asked for. "value of a text field is a
number". Well, those three strings represent a number so there is
nothing inaccurate about my code. There may be an inaccuracy in the
phrasing of the question though.
Also, you've taken "between" to mean "between", which is not what the
ignorant generally intend.
What other way can you define the word between other than to mean between?
The OP may well not want "0000000" or "0004567" so the RegExp way should
probably be like


But that is not what they asked for. And if that is true, they may well
want to accept "123456e0" and "0x000000" and "+1234567".

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Jan 25 '06 #11

This discussion thread is closed

Replies have been disabled for this discussion.