Complex Form Validation

W

W.Sh

Hello Dear Javascript Experts!
I really suck at Javascript at the moment, and I could really use some
assistance...

I have an HTML form, and I'm using javascript to validate the various
entries. Now, I have most of the basic validation figured out, but
there's one input field which requires very strict validation. That
field is the "Address" field.

Besides the obvious checking of whether or not the field is empty, I
also have to make sure that it contains both letters AND numbers, so
that the Street's number isn't left out. Also, I need to make sure that
it is in fact a Street Address, and not a P.O.Box... So the script must
also check to see that the input field does not contain the words
"P.O.Box"...

To sum it up:
1. Field must contain both letters and numbers
2. Field must NOT contain the expression "P.O.Box" (or "p.o.box"..
"PO.Box"... etc)

Any code samples you can provide will be greatly appreciated!!!!!!

Thanks for your time and help!

W.Sh
 
L

Lüpher Cypher

W.Sh said:
Hello Dear Javascript Experts!
I really suck at Javascript at the moment, and I could really use some
assistance...

I have an HTML form, and I'm using javascript to validate the various
entries. Now, I have most of the basic validation figured out, but
there's one input field which requires very strict validation. That
field is the "Address" field.

Besides the obvious checking of whether or not the field is empty, I
also have to make sure that it contains both letters AND numbers, so
that the Street's number isn't left out. Also, I need to make sure that
it is in fact a Street Address, and not a P.O.Box... So the script must
also check to see that the input field does not contain the words
"P.O.Box"...

To sum it up:
1. Field must contain both letters and numbers
2. Field must NOT contain the expression "P.O.Box" (or "p.o.box"..
"PO.Box"... etc)

Any code samples you can provide will be greatly appreciated!!!!!!

Thanks for your time and help!

W.Sh

How about regular expressions?
Say,
1) house number is a number optionally followed by letters
2) Then 1 or more spaces
3)Then street - one or more of letters followed by dot or numbers,
4) followed by 1 or more spaces :)
5) Then optionally apartment - 1 or more numbers+letters

This will allow
"29 W 4 Street"
"193A Evergreen Terrace 26A"
"1 Some Blvd. B6"
"65 4 Street"
etc.

This will deny
"Evergreen Terrace"
"25"
"W 4 Street 29"

Anyways,
1) [0-9]+[a-zA-Z]*
2) \s+
3) (([a-zA-Z]+\.)|([0-9]+))+
4) \s+
5) ([0-9a-zA-Z]+)?

So, we get:

re = /^[0-9]+[a-zA-Z]*\s+(([a-zA-Z]+\.)|([0-9]+))+\s+([0-9a-zA-Z]+)?$/
if (!re.test(address)) {
alert('invalid address');
}

That should do that :)

As for P.O.Box, it could be written in different ways, but in any case
it will be "P" followed by 0 or more spaces, optional ".", and 0 or more
spaces; then same for "O", and then "Box", so:

re = /^(P\s*\.?\s*)(O\s*.?\s*)(Box)$/

That should take care of it :)


luph
 
L

Lee

You said:
W.Sh wrote:
How about regular expressions?
Say,
1) house number is a number optionally followed by letters
2) Then 1 or more spaces
3)Then street - one or more of letters followed by dot or numbers,
4) followed by 1 or more spaces :)
5) Then optionally apartment - 1 or more numbers+letters

This will allow
"29 W 4 Street"
"193A Evergreen Terrace 26A"
"1 Some Blvd. B6"
"65 4 Street"
So, we get:

re = /^[0-9]+[a-zA-Z]*\s+(([a-zA-Z]+\.)|([0-9]+))+\s+([0-9a-zA-Z]+)?$/
if (!re.test(address)) {
alert('invalid address');
}

That should do that :)

That rejects some of your own examples. You haven't allowed for
whitespace in the street name. I know several that have various
punctuation in the number and street name and apartment. Valid
addresses simply don't fit any neat pattern. Fortunately, it's
not part of the OP's requirement, anyway.

As for P.O.Box, it could be written in different ways, but in any case
it will be "P" followed by 0 or more spaces, optional ".", and 0 or more
spaces;

Again, it's not that simple. Some people write "Box 9999". When I
was living on campus a few decades ago, my campus post office box was
"SUPO 9999". And I'm sure you don't want to reject every street name
that contains "po ".

As a final note to the OP, some box addresses look just like suite
numbers. You have no way of knowing whether "1111 N Broadway, Suite
2301" is a penthouse or an anonymous box in the back of a laundromat.
 
M

McKirahan

W.Sh said:
Hello Dear Javascript Experts!
I really suck at Javascript at the moment, and I could really use some
assistance...

I have an HTML form, and I'm using javascript to validate the various
entries. Now, I have most of the basic validation figured out, but
there's one input field which requires very strict validation. That
field is the "Address" field.

Besides the obvious checking of whether or not the field is empty, I
also have to make sure that it contains both letters AND numbers, so
that the Street's number isn't left out. Also, I need to make sure that
it is in fact a Street Address, and not a P.O.Box... So the script must
also check to see that the input field does not contain the words
"P.O.Box"...

To sum it up:
1. Field must contain both letters and numbers
2. Field must NOT contain the expression "P.O.Box" (or "p.o.box"..
"PO.Box"... etc)

Any code samples you can provide will be greatly appreciated!!!!!!

Thanks for your time and help!

W.Sh

You might consider using "UPS U.S. Address Validation"
(from http://www.ups.com/ Online Tools) which would
"Ensure that customer-entered shipping addresses
for the United States are correct."

Post Office boxes are not valid shipping destinations.
 
D

Dr John Stockton

JRS: In article <[email protected]>
, dated Sat, 31 Dec 2005 04:26:08 local, seen in
news:comp.lang.javascript said:
I really suck at Javascript at the moment, and I could really use some
assistance...

This is an international newsgroup, so it is better not to use slang.
I have an HTML form, and I'm using javascript to validate the various
entries. Now, I have most of the basic validation figured out, but
there's one input field which requires very strict validation. That
field is the "Address" field.

Besides the obvious checking of whether or not the field is empty, I
also have to make sure that it contains both letters AND numbers, so
that the Street's number isn't left out.

There are properties which do not have a street number. There are
addresses which do not include a street name.
Also, I need to make sure that
it is in fact a Street Address, and not a P.O.Box... So the script must
also check to see that the input field does not contain the words
"P.O.Box"...

Do you know what P.O.Boxes are called in *all* national languages?
To sum it up:
1. Field must contain both letters and numbers
2. Field must NOT contain the expression "P.O.Box" (or "p.o.box"..
"PO.Box"... etc)

And if Peter Osbert Box opens a shop called P.O.Box Fruiterers, then
your user may be unable to communicate with it. And it would be unwise
to live in Pobox Road.

Tell the user what sort of address you want, and let him bear the
consequences of carelessness. Don't be procrustean.

Any code samples you can provide will be greatly appreciated!!!!!!

<URL:http://www.merlyn.demon.co.uk/js-valid.htm>

<FAQENTRY> If you are only interested in code that works regionally
rather than world-wide, then it would be courteous to indicate that.
 
J

Joost.Jacob

[John]
This is an international newsgroup, so it is better not to use slang.
...
... Don't be procrustean.

why no slang, so peeps don't need a dictionary?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,786
Messages
2,569,626
Members
45,328
Latest member
66Teonna9

Latest Threads

Top