regular expression - 4 OR 6 digits

Posted on 2014-11-02
Last Modified: 2014-11-02

I need a regular expression to validate the user input
it has to be numeric
and it has to be either 4 or 6 digits

i'm not bothered about someone entering all zeros
Question by:websss
  • 3
  • 2
LVL 24

Accepted Solution

mankowitz earned 500 total points
ID: 40418061
this should work: ^\d{4}$|^\d{6}$

Author Comment

ID: 40418065
works for 4 digits but fails for 6
LVL 15

Expert Comment

ID: 40418066

would do the work
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.


Author Comment

ID: 40418067
^[0-9]{4,6}$  allows 5 digits

it needst to be 4 OR 6

Author Closing Comment

ID: 40418069
i had a trailing space.... it worked!
LVL 15

Expert Comment

ID: 40418071
I hope there would have been slight change in comment

and it would have worked right ?

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

777 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