Link to home
Start Free TrialLog in
Avatar of Tammu
Tammu

asked on

Below Regex is not firing when using with jQuery 1.9.1

I am trying to prevent Po Box being allowed in the Shipping Address, here is my code

<script type="text/javascript">
$(document).ready(function($) {
    
	$('input[name=ShipAddress1]').each(function() {
    var pattern = new RegExp('[PO.]*\\s?B(ox)?.*\\d+', 'i');
    if ($(this).val().match(pattern)) {
        $(this).after('<span class="nopo"> We cannot deliver to PO Boxes</span>');
     }
     });
	
});
</script>

What am I missing?

Thanks

Open in new window

Avatar of Terry Woods
Terry Woods
Flag of New Zealand image

Try this:
    var pattern = new RegExp('P[ .]?O[ .]?B([Oo][xX])?[^a-zA-Z]*\\d+', 'i');

Open in new window


If you have any cases that are still being missed, please post them (or a similar case) as an example so we can adjust the pattern.
Avatar of Tammu
Tammu

ASKER

Thanks for replying, here I have made a demo

Demo in jsfiddle

Thanks and I am trying display the message as soon as the text is entered in the text box.

Appreciate it
To explain the problems with the original pattern, this part of the pattern:
[PO.]*
means match any number (including zero) of the specified characters, so it would match an empty string. The remainder of the pattern required a "B" character and at least one number, which would mean these cases would match:
Beautiful Rd Apartment 3
Foo Boulevard Flat 1

Open in new window

Will have a look at the jsfiddle now.
Note the pattern I've provided probably shouldn't have the ignore case flag used with it ('i'), although turning that off will cause it to miss cases like po box.

Anyway, the pattern I provided seems to work ok in jsfiddle. The pattern requires a number to be present before the validation will fail though. Also, the validation is run when the page is loaded, and doesn't revalidate as the user types; did you intend for it to validate as the user types in text?
Avatar of Tammu

ASKER

Thanks for replying so quickly,

basically what I am looking is when the textbox becomes focused or when the cursor is inside the text box I just want a simple message next to the input box saying PO Boxes are not allowed

Thanks again
Avatar of Tammu

ASKER

Hi There, I have modified by script to below now its firing but the form is also getting submitted when I click the submit button How to prevent that.

<script type="text/javascript">
 function cForm(){
 	var input = $('input[name=ShipAddress1]');
    var inputValue = input.val().toUpperCase();
    var poBoxIdentifiers = ['PO BOX', 'POBOX', 'GPOX'];
    for(var i = 0; i < poBoxIdentifiers.length; i++) {
      if (inputValue.indexOf(poBoxIdentifiers[i]) !== -1) {
        alert('We don\'t ship to Po Boxes');
        input.val('').focus();
    }
  } 
 }

</script>

Open in new window


and I added onsubmit event to my form like

<form id="form1" method="post" action="/payment.html" onsubmit="return cForm(this)">

Thanks

Open in new window

SOLUTION
Avatar of Terry Woods
Terry Woods
Flag of New Zealand image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Tammu

ASKER

Thanks