admin管理员组文章数量:1336092
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.
- 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
1 Answer
Reset to default 6This 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.
本文标签: validating home address in jquery or javascript issueStack Overflow
版权声明:本文标题:validating home address in jquery or javascript issue - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742404247a2468493.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论