Check if 4 digits is entered...

Posted on 2011-02-21
Last Modified: 2012-05-11

Have a script that the user must enter a value.
But he/she can enter text or numbers...

I like this script to validate the input from the user to:

MUST enter 4 numbers -> example: 3100 , 3288 , 6655 ...

If user dosent enter 4 numbers, the script must tell user to enter
a 4 digit number...

How do i modify my script to do this ?

<script type="text/javascript">
    var showPrompt = function(href){
     husk_postnr2=prompt("Legg inn postnummer");
            if (husk_postnr2!=""&&husk_postnr2!=null){
        return false;

Open in new window

Question by:team2005
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 15

Expert Comment

by:Jagadishwor Dulal
ID: 34941524
LVL 39

Accepted Solution

Pratima Pharande earned 500 total points
ID: 34941530

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

if (form.postcode.value.length == min)
if (numberFormat.test(form.postcode.value)
return true;
alert("Please enter a 4-digit postcode");
return false;
alert("Please enter a 4-digit postcode");
return false;


Author Closing Comment

ID: 35122761

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Text too large in Chrome 17 83
Add a little space on either side of a table. 12 32
CSS Scroll Center with Static Side. Like EE does. 2 47
Hta File displays dynamic File names 4 65
This article discusses how to create an extensible mechanism for linked drop downs.
Find out what you should include to make the best professional email signature for your organization.
In this tutorial viewers will learn how add a scalable full-width header using CSS3. Create a new HTML document with an internal stylesheet. Set a tiled background.:  Create a new div and name it Header. Position it with position:absolute at the top…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question