By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,949 Members | 1,086 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,949 IT Pros & Developers. It's quick & easy.

Complex Form Validation

P: n/a
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

Dec 31 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
W.Sh wrote:
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
Dec 31 '05 #2

P: n/a
"W.Sh" <ro***********@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
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.
Dec 31 '05 #3

P: n/a
JRS: In article <11**********************@z14g2000cwz.googlegroups .com>
, dated Sat, 31 Dec 2005 04:26:08 local, seen in
news:comp.lang.javascript, W.Sh <ro***********@gmail.com> posted :
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.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Dec 31 '05 #4

P: n/a
On 2005-12-31, W.Sh <ro***********@gmail.com> wrote:
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.
The best way is to make it worth their while to get the address correct.
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,


Not all adresses contain numbers.
There's no simple way to validate addresses.
--

Bye.
Jasen
Jan 1 '06 #5

P: n/a
> [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?

Jan 1 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.