I have this script working and now i just need the alert to pop up when the field is in PHP.
But when i put it in echo"" the onBlur event wont work.

function validate() {
      var valStreet = new RegExp("[pP]{1}[.]*[oO]{1}[.]*[ ]*[bB]{1}[oO]{1}[xX]{1}"); //Anything with "PO BOX" alerted

      if (form.Street.value.match(valStreet) != null) {
         alert("No P O Box Allowed in Shipping Address.");
      }   
   }
   </script>
</head>
<body><form name="test">
Street: <input type="text" name="street" value="" onBlur="validate();"><br>
<input type="button" name="test" value="test">
</form>
<?PHP echo"<input type='text' name='street' onBlur=\"validate();\" value=$street >"; ?>

//Something wrong with the onBlur Code!!!!

What is the correct way to get onBlur to work when in PHP?????

I just fixed your double-quotes and it worked

<html>
<head>
<script type="text/javascript">
function validate() {
  var valStreet = new RegExp("[pP]{1}[.]*[oO]{1}[.]*[ ]*[bB]{1}[oO]{1}[xX]{1}"); //Anything with "PO BOX" alerted

  if (form.Street.value.match(valStreet) != null) {
    alert("No P O Box Allowed in Shipping Address.");
  }   
}
</script>
</head>
<body>
<?php
$street = 'ez';
?>
<form name="test">
  Street:
  <?php print "<input type=\"text\" name=\"street\" onBlur=\"validate();\" value=\"$street\" >"; ?>
  
  <!--<input type="text" name="street" value="" onBlur="validate();">-->
  <br>
  <input type="button" name="test" value="test">
</form>
</body>
</html>

Did you know that with regular expressions you can do a case-insensitive modifier and not have to do dB type stuff.
Something like this ...

.match( /p.*?o.*?b.*?o.*?x.*/i );

...should work, but RegEx is always tricky and I didn't actually test the above
For that matter, you could probably get away with just this ...

.match( /.*?box/i ); // pobox, po box, post office box, mail box, postal box (all in upper and/or lower case)

Enjoy

This article has been dead for over six months. Start a new discussion instead.