Forum Moderators: open

Message Too Old, No Replies

How to validate an address field of a form

         

JoeVodkaSauce

3:40 am on Oct 30, 2007 (gmt 0)

10+ Year Member



I've seen some postings about form validation and it seems to be a pretty common function for javascript to handle. I have two issues I'm trying to tackle:

1 - I'm VERY novice at Javascript.
2 - I'm trying to validate a form to NOT allow PO Boxes (i.e. PO Box, P.O. Box, Post Office Box) to be entered as an address in a page I've written. This validation need sot be done and some error or message given to the user before the form can be submitted. I guess some type of "even handler"? (whatever that means) to stop the submission or let it go through.

Can anyone point me in a direction to start looking into doing this? I came across a lot of validation script examples but nothing I saw had examples like what I am looking for. Mostly for numeric or null value inputs.

Thanks in advance for any thoughts/direction!

Fotiman

3:20 pm on Oct 30, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Well, most importantly, JavaScript should be used as an "enhancement", not a requirement. So your form should not be dependent on JavaScript alone to perform your validation. Instead, whatever server-side language you use to process the form (ASP, PHP, etc.) should also perform the validation. Otherwise, people will be able to bypass your validation simply by disabling JavaScript.

JoeVodkaSauce

3:57 pm on Oct 30, 2007 (gmt 0)

10+ Year Member



Good point! I've trying to see what other options there may be for my validation.

rocknbil

4:43 am on Oct 31, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



In both cases what you will be looking for is some sort of regular expression match (regexp) on P.O. Box, one that catches all of those characters whether or not they are all present including spaces, and is case-insensitive. You can do it in Javascript as an aid to the visitor so it catches them before submitting, and server-side in case someone's circumnavigating your J.S. (intentionally or not.)