Validate form for 4-digit integer

B

Bunyip Bluegum

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
 
L

Lasse Reichstein Nielsen

Bunyip Bluegum said:
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
 
D

David W. Burhans

Bunyip Bluegum said:
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;
}
}
 
D

Dr John Stockton

JRS: In article <[email protected]>, seen
in David W. Burhans
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.
 
G

George M Jempty

Dr said:
JRS: In article <[email protected]>, seen
in David W. Burhans




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.
 
D

Dr John Stockton

JRS: In article <[email protected]>, seen in
news:comp.lang.javascript said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top