Re: beginner: regex how to check that value is only numbers



JRS: In article <Iq-dnTDq4vi8nEveRVn-hA@xxxxxxxxxxx>, dated Tue, 24 Jan
2006 05:28:01 remote, seen in news:comp.lang.javascript, Randy Webb
<HikksNotAtHome@xxxxxxx> 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&&(numToCheck.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.
.



Relevant Pages

  • Re: explain what this means
    ... function getObj{var T ... jscr maths, dates, sources. ... TP/BP/Delphi/jscr/&c, FAQ items, links. ... Prev by Date: ...
    (comp.lang.javascript)
  • Re: get ip
    ... ISTM you have a colon too many. ... jscr maths, dates, sources. ... TP/BP/Delphi/jscr/&c, FAQ items, links. ... Prev by Date: ...
    (microsoft.public.scripting.jscript)
  • Re: Convert to VBScript to JavaScript ?
    ... ISTM add 7 for next Friday. ... jscr maths, dates, sources. ... TP/BP/Delphi/jscr/&c, FAQ items, links. ... Prev by Date: ...
    (comp.lang.javascript)
  • Re: Number problem
    ... Lasse Edsvik posted: ... jscr maths, dates, sources. ... TP/BP/Delphi/jscr/&c, FAQ items, links. ... Prev by Date: ...
    (microsoft.public.scripting.vbscript)
  • Re: time zone
    ... One need only go back by the 0..6 days given by getDay() from the last ... jscr maths, dates, sources. ... TP/BP/Delphi/jscr/&c, FAQ items, links. ... Prev by Date: ...
    (comp.lang.javascript)