Solved

regular expression - 4 OR 6 digits

Posted on 2014-11-02
6
178 Views
Last Modified: 2014-11-02
Hi

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
0
Comment
Question by:websss
  • 3
  • 2
6 Comments
 
LVL 24

Accepted Solution

by:
mankowitz earned 500 total points
Comment Utility
this should work: ^\d{4}$|^\d{6}$
0
 

Author Comment

by:websss
Comment Utility
works for 4 digits but fails for 6
0
 
LVL 15

Expert Comment

by:Insoftservice
Comment Utility
^[0-9]{4,6}$  
OR
/^\d{4|6}$/

would do the work
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

Author Comment

by:websss
Comment Utility
^[0-9]{4,6}$  allows 5 digits

it needst to be 4 OR 6
0
 

Author Closing Comment

by:websss
Comment Utility
i had a trailing space.... it worked!
0
 
LVL 15

Expert Comment

by:Insoftservice
Comment Utility
I hope there would have been slight change in comment

^[0-9]{4,7}$
and it would have worked right ?
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Do you hate spam? I do, and I am willing to bet you do as well. I often wonder, though, "if people hate spam so much, why do they still post their email addresses on the web?" I'm not talking about a plain-text posting here. I am referring to the fa…
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…

762 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

13 Experts available now in Live!

Get 1:1 Help Now