最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

validating home address in jquery or javascript issue - Stack Overflow

programmeradmin20浏览0评论

I am trying to validate home address that is street address. But it returns false everytime. Here is my code

validateAddress: function (val) {
            console.log('val: ' + val);
            var streetregex = /^[a-zA-Z0-9-\/] ?([a-zA-Z0-9-\/]|[a-zA-Z0-9-\/] )*[a-zA-Z0-9-\/]$/;
            if ( streetregex.test(val) ) {
                console.log('true');
            } else {
                console.log('false');
            }

        }

val has street address in this format street name streetnumber, city.

How can I fix it so it correctly validates my address?

Update

Here is my DEMO

if you give address like this Street name 18, Helsinki. It returns false whereas I want it to return true for these sort of addresses.

I am trying to validate home address that is street address. But it returns false everytime. Here is my code

validateAddress: function (val) {
            console.log('val: ' + val);
            var streetregex = /^[a-zA-Z0-9-\/] ?([a-zA-Z0-9-\/]|[a-zA-Z0-9-\/] )*[a-zA-Z0-9-\/]$/;
            if ( streetregex.test(val) ) {
                console.log('true');
            } else {
                console.log('false');
            }

        }

val has street address in this format street name streetnumber, city.

How can I fix it so it correctly validates my address?

Update

Here is my DEMO

if you give address like this Street name 18, Helsinki. It returns false whereas I want it to return true for these sort of addresses.

Share edited Nov 5, 2012 at 6:34 Om3ga asked Nov 5, 2012 at 6:23 Om3gaOm3ga 33k45 gold badges149 silver badges230 bronze badges 10
  • Could you probably point us to jsfiddle? – Srinivas Commented Nov 5, 2012 at 6:26
  • Street name only appears to be one character.. – Daedalus Commented Nov 5, 2012 at 6:28
  • The question is way too vaguely phrased, given the plexity of real-life addresses. For example, your format (not the regexp) won't accept "123 Main St., New York". – Dan Dascalescu Commented Nov 5, 2012 at 6:31
  • @al0neevenings And what about the data you're using to test it..? – Daedalus Commented Nov 5, 2012 at 6:35
  • 1 I don't think you can validate even your minimal (and unusual) street address requirements with any certainty using only a regular expression, though you might use one for tokenisation. You probably need to parse the parts manually, e.g. how will you cope with an address of "7th Avenue 24a, 1770"? Yes "1770" is a place name. – RobG Commented Nov 5, 2012 at 6:45
 |  Show 5 more ments

1 Answer 1

Reset to default 6

This regexp will do what you ask for, but I doubt it's useful for any real-life application:

var regexp = /^[\w\s.-]+\d+,\s*[\w\s.-]+$/;
console.log(regexp.test('Main St. 123, New York'));
console.log(regexp.test('123 Wall St., New York'));

​ Fiddle at http://jsfiddle/dandv/fxxTK/5/

The way it works is:

match a sequence of alphanumeric characters, spaces, period or dash, e.g. "Abel-Johnson St."
followed by a number
followed by a ma
followed by another sequence of alphanumeric characters, spaces, period or dash (e.g. "St. Mary-Helen")

This is very frail, however, and you should probably simply not attempt to validate street addresses.

发布评论

评论列表(0)

  1. 暂无评论