Flexible Form Validation

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • draney
    New Member
    • Aug 2008
    • 8

    Flexible Form Validation

    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.
  • gits
    Recognized Expert Moderator Expert
    • May 2007
    • 5390

    #2
    hi ...

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

    [CODE=javascript]var s = 'P. O. Box';
    var re = /p{1,1}(.| ){0,}o{1,1}(.| ){0,}(box){0,}/i;

    alert(re.test(s ));[/CODE]
    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

    Comment

    • draney
      New Member
      • Aug 2008
      • 8

      #3
      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?

      Code:
      var s  = 'P. O. Box';
      var re = /p{1,1}(.| ){0,}o{1,1}(.| ){0,}(box){0,}/i;
      
      function validForm(my_form) {
        if (my_form.contactname.value == "") {
          alert("You must enter a Contact Name!");
          my_form.contactname.focus();
          return false;
        }
      
        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"
      
        if (re.test(s)) {
          return confirm(msg);
          my_form.shipstreet1.focus();
        }

      Comment

      • draney
        New Member
        • Aug 2008
        • 8

        #4
        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!!

        Code:
        function validForm(my_form) {
          if (my_form.contactname.value == "") {
            alert("You must enter a Contact Name!");
            my_form.contactname.focus();
            return false;
          }
        
        	var s  = my_form.shipstreet1.value;
        	var re = /p{1,1}(.| ){0,}o{1,1}(.| ){0,}(box){0,}/i;
        	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 \!\!"
        
        	if (re.test(s)) {
        		if (confirm(msg)) {
        		return true;
        		}
        		else
        		{
        		my_form.shipstreet1.focus();
        		my_form.shipstreet1.select();
        		return false;
        		}
        	}
        }

        Comment

        • gits
          Recognized Expert Moderator Expert
          • May 2007
          • 5390

          #5
          yes ... it looks quite good ... well done :) post back to the forum anytime you have more questions ...

          kind regards

          Comment

          • draney
            New Member
            • Aug 2008
            • 8

            #6
            Thanks again for your help. Much Appreciated.

            Comment

            • draney
              New Member
              • Aug 2008
              • 8

              #7
              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?

              Comment

              Working...