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

Validate form for 4-digit integer

P: n/a
I have a text field in a form which I need to check to see that only a
4-digit integer has been entered. The field has MAXLENGTH=4 and I'm
using this to check for length:

function checkpostcode(form) {
var min=4;
if (form.postcode.value.length < min) {
alert("Please enter a 4-digit postcode");
return false;
}
else return true;
}

That works fine but how can I extend that to check that the input
consists only of four digits. Any help would be much appreciated.

Bunyip

Jul 20 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Bunyip Bluegum <bunyip@australia> writes:
That works fine but how can I extend that to check that the input
consists only of four digits. Any help would be much appreciated.


Sounds like a job for regular expressions;

if (/^\d{4}$/.test(form.elementsp['postcode'].value)) { ...

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #2

P: n/a
Bunyip Bluegum <bunyip@australia> wrote in message news:<kr********************************@4ax.com>. ..
I have a text field in a form which I need to check to see that only a
4-digit integer has been entered. The field has MAXLENGTH=4 and I'm
using this to check for length:

function checkpostcode(form) {
var min=4;
if (form.postcode.value.length < min) {
alert("Please enter a 4-digit postcode");
return false;
}
else return true;
}

That works fine but how can I extend that to check that the input
consists only of four digits. Any help would be much appreciated.


Use a regular expression to validate the format of a string. Here is an example:

function checkpostcode(form)
{
var min = 4;
var numberFormat = /^\d{4}$/;

if (form.postcode.value.length == min)
{
if (numberFormat.test(form.postcode.value)
{
return true;
}
else
{
alert("Please enter a 4-digit postcode");
return false;
}
}
else
{
alert("Please enter a 4-digit postcode");
return false;
}
}
Jul 20 '05 #3

P: n/a
JRS: In article <ff**************************@posting.google.com >, seen
in news:comp.lang.javascript, David W. Burhans
<da*************@yahoo.com> posted at Tue, 16 Sep 2003 10:31:58 :-
function checkpostcode(form)
{
var min = 4;
var numberFormat = /^\d{4}$/;

if (form.postcode.value.length == min)
{
if (numberFormat.test(form.postcode.value)

There is no need to pre-test the length explicitly when the RegExp does
it perfectly well.

If the value entered was generally the wrong length, it is possible that
insignificant execution time might be saved by the pre-test, at a cost
of increased download time.

If doing the pre-test, the RegExp test should be /\D/ since any non-
digit is immediately fatal; this certainly saves insignificant time.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> JS maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.
Jul 20 '05 #4

P: n/a
Dr John Stockton wrote:
JRS: In article <ff**************************@posting.google.com >, seen
in news:comp.lang.javascript, David W. Burhans
<da*************@yahoo.com> posted at Tue, 16 Sep 2003 10:31:58 :-

function checkpostcode(form)
{
var min = 4;
var numberFormat = /^\d{4}$/;

if (form.postcode.value.length == min)
{
if (numberFormat.test(form.postcode.value)


There is no need to pre-test the length explicitly when the RegExp does
it perfectly well.


Additionally there is something else I believe is noteworthy. The OP
speaks of a 4-digit postal code. Can the first digit be a zero? I'm
guessing yes, and we are not going to be doing any arithmetic using the
postal code.

But if the first digit should NOT be a zero, then the regular expression
would be:

/^[1-9]\d{3}$/

This of course would be particularly important if arithmetic were to be
performed, because a leading zero would result in the number being
evaluated as an octal.

Jul 20 '05 #5

P: n/a
JRS: In article <3f**********@newsfeed.slurp.net>, seen in
news:comp.lang.javascript, George M Jempty <gj*****@intergate.com>
posted at Thu, 18 Sep 2003 09:18:08 :-

But if the first digit should NOT be a zero, then the regular expression
would be:

/^[1-9]\d{3}$/

This of course would be particularly important if arithmetic were to be
performed, because a leading zero would result in the number being
evaluated as an octal.


Only by FAQ-ignorers. Never use parseInt without a second parameter,
unless you want to allow octal conversion. AFAICS, all other methods of
conversion allow only decimal & hexadecimal, except for parseFloat which
only allows decimal.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> JS maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.
Jul 20 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.