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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
In this tutorial viewers will learn how to code links for mobile sites that, once clicked, send a call or text to a specified number. For a telephone link (once clicked, calls a number), begin with a normal "<a href=" link tag. For the href, specify…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

895 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now