473,486 Members | 2,117 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 26606
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
2478
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
1145
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
4452
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
4782
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
4267
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
1240
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
359
by: jayparaiso | last post by:
Hi! How to validate check box and drop down menu in one form?
0
1164
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
3383
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
7100
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6964
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7126
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7330
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5434
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4559
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3070
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3070
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1378
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.