Solved

regular expression - 4 OR 6 digits

Posted on 2014-11-02
6
185 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
ID: 40418061
this should work: ^\d{4}$|^\d{6}$
0
 

Author Comment

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

Expert Comment

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

would do the work
0
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.

 

Author Comment

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

it needst to be 4 OR 6
0
 

Author Closing Comment

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

Expert Comment

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

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

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

Whatever be the reason, if you are working on web development side,  you will need day-today validation codes like email validation, date validation , IP address validation, phone validation on any of the edit page or say at the time of registration…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
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…

911 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

22 Experts available now in Live!

Get 1:1 Help Now