473,320 Members | 1,732 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Validate form for 4-digit integer

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
5 26533
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Mattia | last post by:
Hi! I would like to validate a fiels in a form, but the code I wrote doesn't seem to work: HTML: <form name="formNome" method="post" action="/index.php" onSubmit="return...
1
by: Omonoia | last post by:
Dear all, I have a form with some text fields. Below you can see its declaration <form method="POST" name="ConferenceData" action="add_conference.asp" onsubmit="return ValidateForm(this)"> ...
1
by: mhawkins19 | last post by:
I have a form built and on the onclick event I validate all of the fields and then if the form is ok, on the submit event I run a javascript function to set a cookie and download a file from the...
15
by: simonmarkjones | last post by:
I want to validate my form using a BeforeUpdate event. However now that i call my code with a beforeupdate it wont let me go to next or previous records. What code should i put in o allow me...
4
by: Wysiwyg | last post by:
I need to validate a form to ensure that all of the fields add up correctly. I can't do this while the user is entering data since validation needs to be done after the entry is completed. What's...
2
by: Adrian | last post by:
Hi everyone I have a web form where the validation works pefectly I copied the file, renamed it, and all of a sudden the validation stopped working(on the copied form). I have two panels on...
1
by: jayparaiso | last post by:
Hi! How to validate check box and drop down menu in one form?
0
by: chaitanya02 | last post by:
I have designed a form to take input of email name, password, addres,, etc. When clicked on submite- it has to validate these values with the values of that stored in Salesforce, for the same person....
4
by: jmartmem | last post by:
Greetings, I have an ASP page containing a Record Insertion Form with a number of fields (mostly text fields and list/menu fields) linked to an Access database. I've created an onsubmit behavior...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.