473,378 Members | 1,507 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Flexible Form Validation

8
I need a javascript form validation to detect the entry of what would appear to be a PO Box in a Street Address field. We use UPS for shipping and they can't deliver to a PO Box. I would like to know how to detect any of the following entries as the start of the address entry (not case sensitive):
PO Box
P.O.
P O Box
P. O.
I know there are other possibilities, but this would detect 90% of the PO Box address entries and this doesn't have to be completely foolproof.

If one of the strings above is detected, I would like to give the user an alert message with an option to continue (override the warning) with the address as entered. I would like the alert message to say:
"It appears that you have entered a PO Box in the Street Address field and UPS is unable to deliver to a PO Box."
and offer 2 buttons:
Change Address - return to form with focus on Address field
Continue Anyway - submit the form without making any changes

I would like this be added to the end of my current validation script as the last check before the form is submitted.

THANKS!! in advance for your help with this.
Aug 15 '08 #1
6 1999
gits
5,390 Expert Mod 4TB
hi ...

you may use a regExp for such purposes, here is an example:

Expand|Select|Wrap|Line Numbers
  1. var s  = 'P. O. Box';
  2. var re = /p{1,1}(.| ){0,}o{1,1}(.| ){0,}(box){0,}/i;
  3.  
  4. alert(re.test(s));
such a construct could be used in a function that you call onchange of the address-field and in case the test is true then you need to construct a dialog with your alert message. you may use the javascript confirm() method or a custom DHTML dialog for this ... try something and come back in case you have specific problems with the implementation ... you should have a starting point now :)

kind regards
Aug 16 '08 #2
draney
8
I know this is probably not correct, but I really am a super beginner with javascript, so forgive my ignorance. Here is my validation script with my attempt at adding your modifications. Is this even close?

Expand|Select|Wrap|Line Numbers
  1. var s  = 'P. O. Box';
  2. var re = /p{1,1}(.| ){0,}o{1,1}(.| ){0,}(box){0,}/i;
  3.  
  4. function validForm(my_form) {
  5.   if (my_form.contactname.value == "") {
  6.     alert("You must enter a Contact Name!");
  7.     my_form.contactname.focus();
  8.     return false;
  9.   }
  10.  
  11.   msg = "Your shipping address appears to be a PO Box: \n\n Street Address: " + my_form.shipstreet1.value + "\n\n UPS cannot deliver to a PO Box \n click 'CANCEL' if you need to change the address"
  12.  
  13.   if (re.test(s)) {
  14.     return confirm(msg);
  15.     my_form.shipstreet1.focus();
  16.   }
  17.  
Aug 16 '08 #3
draney
8
I have been playing with it some more and testing it. It seems to be working very well. Thanks You for getting me started!!! I am pasting the code below and would appreciate it if you could let me know if this looks good to you? I would like to avoid any potential problems that could come up down the road.

Thanks again!!

Expand|Select|Wrap|Line Numbers
  1. function validForm(my_form) {
  2.   if (my_form.contactname.value == "") {
  3.     alert("You must enter a Contact Name!");
  4.     my_form.contactname.focus();
  5.     return false;
  6.   }
  7.  
  8.     var s  = my_form.shipstreet1.value;
  9.     var re = /p{1,1}(.| ){0,}o{1,1}(.| ){0,}(box){0,}/i;
  10.     msg = "SHOULD WE CONTINUE WITH THIS SHIPPING ADDRESS? \n It appears to be a PO Box: \n\n     Street Address: " + my_form.shipstreet1.value + "\n\n UPS CANNOT DELIVER TO A PO BOX \!\!"
  11.  
  12.     if (re.test(s)) {
  13.         if (confirm(msg)) {
  14.         return true;
  15.         }
  16.         else
  17.         {
  18.         my_form.shipstreet1.focus();
  19.         my_form.shipstreet1.select();
  20.         return false;
  21.         }
  22.     }
  23. }
  24.  
  25.  
Aug 17 '08 #4
gits
5,390 Expert Mod 4TB
yes ... it looks quite good ... well done :) post back to the forum anytime you have more questions ...

kind regards
Aug 18 '08 #5
draney
8
Thanks again for your help. Much Appreciated.
Aug 18 '08 #6
draney
8
I have been using this script for a couple of years. It is working pretty well, but I would like to see if we could improve the test to eliminate some of the false positives. When someone enters an address like Port Smith Rd. the script alerts that it looks like a PO Box because it starts with "Po".

Customers are concerned about security when they get a popup warning and many are afraid to continue afterward, so it could be impacting sales.

Does anyone have a good suggestion to help with this?
Nov 15 '10 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: TG | last post by:
I have a validation form that must behave differently based on the results of a PHP validation check. I have a post command at the top of my form that calls itself. I don't leave the form when...
16
by: Hosh | last post by:
I have a form on a webpage and want to use JavaScript validation for the form fields. I have searched the web for form validation scripts and have come up with scripts that only validate...
9
by: julie.siebel | last post by:
Hello all! As embarrassing as it is to admit this, I've been designing db driven websites using javascript and vbscript for about 6-7 years now, and I am *horrible* at form validation. To be...
27
by: Chris | last post by:
Hi, I have a form for uploading documents and inserting the data into a mysql db. I would like to validate the form. I have tried a couple of Javascript form validation functions, but it...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.