10

I have javascript code for checking the zipcode

var regexObj =
/^(?=[^-]*-?[^-]*$)[0-9-]*[0-9]$/;

I need to add one more condition to this,ie

make it so that user has to enter a minimum of 3 characters

Can any one say, how can i modify my regular expression for that

Sean Vieira
  • 155,703
  • 32
  • 311
  • 293
Linto P D
  • 8,839
  • 7
  • 30
  • 39
  • You don't need a regex to do that. Just add an additional check, `string.length >= 3` That's probably gonna be faster than any regex, too. – kijin Jan 07 '11 at 22:38
  • @kijin It's quite sensible to match the pattern using a regex. Perhaps you misread the question, perhaps I did. – moinudin Jan 07 '11 at 22:57

4 Answers4

18
/^(?=[^-]*-?[^-]*$)[0-9-]*[0-9]$/

is equivalent to

/^[0-9-]*[0-9]$/

You can add a length check in the same pass without requiring lookahead

/^[0-9-]{2,}[0-9]$/

That's a minimum of 3 characters, the last being a digit and the rest being digits and -. See http://www.rubular.com/r/oa9wVxggz0

You might also want to restrict the first character from being a -. You might also want to require 3 digits, without counting the - as one of the required 3 characters. Putting these together we would get:

/^[0-9]-*[0-9][0-9-]*[0-9]$/

See http://www.rubular.com/r/Qhl843Txib

moinudin
  • 134,091
  • 45
  • 190
  • 216
13

Why in the world do you need a regex to check the length of a string?

var testString1 = 'this is long enough';
alert(testString1.length >= 3); // true

var testString2 = 'no';
alert(testString2.length >= 3); // false
Matt Ball
  • 354,903
  • 100
  • 647
  • 710
  • 2
    Easiest reason I can think of is if he doesn't want to add an additional check since he will already be doing a RegEx.IsMatch might as well check minimum length as well instead of adding an additional method call. – Adrian Jan 07 '11 at 22:41
  • 3
    @downvoters: any comments? Seriously, a regex is the **wrong tool for the job**. – Matt Ball Jan 07 '11 at 22:43
  • 7
    Wrong tool for checking the length only, but OP is also matching a pattern. – moinudin Jan 07 '11 at 22:51
3

In HTML5, you can use a pattern.

In your example, you would do the following, where 3 is the minimum value and anything after the comma would be a maximum value (in your case, you don't have a maximum value):

<input pattern=".{3,}">
Community
  • 1
  • 1
thecoolmacdude
  • 2,036
  • 1
  • 23
  • 38
1

Are you sure this is for checking "ZipCode" (uniquely American)?
Wikipedia: "ZIP codes are a system of postal codes used by the United States Postal Service (USPS) since 1963. The term ZIP, an acronym for Zone Improvement Plan,[1] is properly written in capital letters and was chosen to suggest that the mail travels more efficiently, and therefore more quickly, when senders use the code in the postal address. The basic format consists of five decimal numerical digits. An extended ZIP+4 code, introduced in the 1980s, includes the five digits of the ZIP code, a hyphen, and four more digits that determine a more precise location than the ZIP code alone."
/^(\d{5})(-\d{4})?$/