473,372 Members | 1,055 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,372 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 26551
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...
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...
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.