2

I would like to validate residence address in the JavaScript using regex, but I dont know much about the regex, what I have tried is to build my own regex (/^[a-zA-Z\s](\d)?$/) but it doesn't seem to work properly.

What I want to achieve is to allow letters, spaces and at least one digit (thats required) and also there should be a possibility to insert the slash / but it shouldnt be required.

Could anyone help me with that?

Scott
  • 5,991
  • 15
  • 35
  • 42

1 Answers1

5

I'll get you started, but you'll find yourself becoming more specific as you get accustomed to regular expressions. First, let's examine your regex:

/^[a-zA-Z\s](\d)?$/

The essential thing to note here is that this regex will only match, at most, a two-character string! The character class, [ ... ], matches a single character: in your case, letters and whitespace. You need to combine this with what's called a quantifier, e.g. * meaning "zero or more", + meaning "one or more", and ? meaning "zero or one". You've used a quantifier elsewhere, (\d)?, where you're saying "zero or one" digit character. But what you really want looks more like this:

/^[a-zA-Z\s\d\/]+$/

Here, we're saying "one or more" letters, whitespace, digits, or slashes (note that the forward slash must be escaped using a backslash).

Finally, you say want to require "at least one" digit. This can be achieved with a more advanced construct in regular expressions, called "lookaround assertions." You want this:

/^(?=.*\d)[a-zA-Z\s\d\/]+$/

That, in particular, is a positive lookahead assertion, which you can research yourself. An alternate way of doing this, without lookahead assertions, would be:

/^[a-zA-Z\s\d\/]*\d[a-zA-Z\s\d\/]*$/

This is obviously more complicated, but when you're able to understand this, you know you're well on your way to understanding regular expressions. Good luck!

Andrew Cheong
  • 29,362
  • 15
  • 90
  • 145
  • Wow. Thats a briliant answer, thank you so much for your explanation! – Scott Oct 31 '12 at 08:13
  • By the way: The above regex examples should work in PHP's preg_match or it should be rewritten then? I know that PHP's regex and the JS one, are kinda similar, but when it comes to unicode regex - JavaScript got problems with that. – Scott Oct 31 '12 at 08:17
  • @Scott - The above regular expressions should work the same in PHP as they do in JS, except that in JS, for example using the `replace` method, you wouldn't pass in the expression as a (quoted) string, while in PHP you would. – Andrew Cheong Oct 31 '12 at 08:37
  • Oh, okay, thank you. Now I'm trying to build another regex on what you have wrote, it should check if passed string has at least one digit (if yes - pass, otherwise throw error) so I should go like this `/^(?=.*\d)+$/` then ? – Scott Oct 31 '12 at 08:50
  • @Scott - One correction, first: `/^(?=.*\d)$/` (drop the quantifier). But actually a better solution here is simply `/\d/`! See, a lookahead assertion was useful in the _previous_ case because you were making a sort of "AND" statement, _i.e._ yada yada characters _AND_ at least one digit character. The "zero-width" nature of lookahead assertions lets you "peek" ahead, yet "retain your position" in the string; thus you can check more than one condition. In your latest case however you need only to check a single condition: the simplest is looking for a `\d` and removing the anchors `^` and `$`. – Andrew Cheong Oct 31 '12 at 08:56
  • Yes, but I would like to have ANY character in my field. When I do `/\d/` regex validation, it only allows digits in the field, while I want it to allow any character, plus require (at least) one digit. – Scott Oct 31 '12 at 09:03
  • @Scott - Are you sure? That shouldn't be the case. `/\d/` should match if there is a `\d` _anywhere_ in the subject string. The anchors `^` and `$` mean "beginning-of-string" and "end-of-string", and that's why doing `/^\d$/`, is like saying, "The _whole_ string is _only_ a single `\d`." But yeah, without those anchors, the test should pass if there is a digit anywhere, regardless what the rest of the string looks like. If you find that this isn't the case, then post some code so we can examine it. (If I don't respond soon then I may have gone to bed; I'll respond tomorrow.) – Andrew Cheong Oct 31 '12 at 09:07
  • Sorry it seems like the code wasn't updated on the server while I was testing it, of course the `/\d/` is working fine! Thank you :)! – Scott Oct 31 '12 at 09:11
  • @Scott - Good to hear. Believe me I'm more familiar than I'd like to admit with the clicked-refresh-too-early "bug". – Andrew Cheong Oct 31 '12 at 09:14