validating home address in jquery or javascript issue - Stack Overflow

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

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742404248a4437521.html

相关推荐

  • validating home address in jquery or javascript issue - Stack Overflow

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

    4小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信