0

I want to validate zipcode or pincode in address fields. that's why I am trying to write regular express which is just except a-z (upper and lower both), 0-9 numbers, round brackets (eg. ()) and hyphen - and space. But some rules must be followed like single white space can not be on first position, two or more white space can not be allowed.

some of invalid entries

1254588
125  255
((125))  255
125--255
(125) (255)
125>2458
EL$ 2458
@L$ 2458
Toto
  • 89,455
  • 62
  • 89
  • 125
Manish Sapkal
  • 5,591
  • 8
  • 45
  • 74
  • "Some rules" is a bit unspecific. Please explain the precise rules, then it's going to be easy to construct a suitable regex. – Tim Pietzcker Feb 06 '14 at 06:37
  • For your information : http://stackoverflow.com/questions/15747087/regular-expression-to-verify-zip-code-and-checking-for-invalid-characters – Yuvi Feb 06 '14 at 06:38
  • List all the rules in detail, without that we're just guessing. – paxdiablo Feb 06 '14 at 06:38

1 Answers1

3

If those are all the rules that matter, it's easy:

^                  # Start of string
(?! )              # First character mustn't be space
(?!.*  )           # No two spaces in a row
[A-Za-z0-9 ()-]*   # Match any number of these allowed characters
$                  # End of string

or, for JavaScript:

/^(?! )(?!.*  )[A-Za-z0-9 ()-]*$/

but I'm guessing that strings like "))))((((", "-------", "A" or even "" shouldn't actually be matched, but are allowed by your rules.

Tim Pietzcker
  • 328,213
  • 58
  • 503
  • 561
  • thanks @tim pietzcher, your doubt is very true. and I also don't want this values. can you consider this in same expression? – Manish Sapkal Feb 06 '14 at 07:00
  • @ManishSapkal: Yes, if you specify the exact rules that should be applied to the regex. – Tim Pietzcker Feb 06 '14 at 07:06
  • pietzcher, I want user can enter only valid zipcode which is as follows : (215) 45878, 2585 55 4587 ED 261-445589. means two space, hyphen, start/end round brackets in a row now allowed. A-Z (upper/lower) and 0-9 only allowed. initially it is acceptable for me. – Manish Sapkal Feb 06 '14 at 07:10
  • These rules do not rule out `----` or `()()()` or `)a(` or `-( -)`... you need to be more precise than that. – Tim Pietzcker Feb 06 '14 at 07:14
  • pietzcher yes. But i can't do that, because I am not much aware with regex. can you help me out to do this? – Manish Sapkal Feb 06 '14 at 07:16
  • You don't need to know regex, you need to define the problem exactly by stating the rules what strings may or may not match. I can then translate them to regex. Your question currently is like "Please nail this picture to this wall", and I could nail it upside-down, face-backwards in the lower left corner of the wall because you haven't said how exactly you want it hanged. Computers need precise instructions, and you can only give them precise instructions if the requirements are absolutely clear. In your case, they aren't. – Tim Pietzcker Feb 06 '14 at 07:23
  • @Pietzcker, sorry for inconvenience due to my bed English, But I just want to validate zipcode/pincode/postal code for any city. (may be this is different in various countries and cities), but my basic knowledge is, it should be accept alphabet and numbers with some special characters eg. space, hyphen and (). and this special characters must not be twice one after one (means '((' or '))' or '--' can't be acceptable. – Manish Sapkal Feb 06 '14 at 07:29
  • Don't do this. How can you possibly know what a legal Zip code is for all the countries on earth? You can't perform any meaningful validation here, you will only annoy your users and prevent them from entering a valid code that you haven't thought of, and still allow them to enter invalid codes (for example, in Germany, postcode always have exactly five digits, no more, no less). Some countries may not have zipcodes at all. – Tim Pietzcker Feb 06 '14 at 08:42
  • that's fine. but still I need this validations because avoiding totally unused data in my system. – Manish Sapkal Feb 06 '14 at 09:02
  • So you would prefer having wrong data to wasting a couple of bytes? – Tim Pietzcker Feb 06 '14 at 09:19
  • yes @Tim. no data is better then wrong data for my application. – Manish Sapkal Feb 06 '14 at 09:23
  • I give up. You can change the regex to `/^(?! )(?!.*([ ()-])\1)[A-Za-z0-9 ()-]*$/` to avoid doubles like `--` or `((`, but you're still not going to achieve what you want. Even worse, you're getting a false sense of security, and you're still making it hard on your users unnecessarily. – Tim Pietzcker Feb 06 '14 at 09:33