Link to home
Start Free TrialLog in
Avatar of Jon Imms
Jon ImmsFlag for United States of America

asked on

Alert box, if certain word is entered in text box.

Hi there,

I currently have a WordPress website,  which is using the WP-Job Manager plugin on the home page.

Everything is working as it should,  but not just learned that we cannot hire out of California.   I've taken out all the jobs we have for that, but we also have a sticky job which always appears at the top of the listings, regardless of what city, state, zip code is entered.
the sticky job location is just USA, and uses google api, so it will appear top of any location entered.

I just cannot hide it for California.

I was wondering if anything had similar problems or different solutions.   Maybe, if California was typed, a Javascript alert box appears, saying we don
t hire from California?  

Just at a loss as for how to fix this.  Any ideas?

<< Website >>
Avatar of Zakaria Acharki
Zakaria Acharki
Flag of Morocco image

Hi Jon, In which step you want to show up the alert? I mean when the user clicks "APPLY" or after the redirect in the job offer details?
Avatar of Jon Imms

ASKER

User generated image
So in the first box, "Enter City, State or ZipCode".  Ideally, if someone enters the word cali, ca, or California, then the alert pops up .. something like, "we currently do not hire from the state of California. "
So it could be something like :

$(function() {
  $('body').on('blur', '#search_location', function() {
    var excluded_cities = ['ca', 'cali', 'california'];

    if ($.inArray($(this).val().toLowerCase(), excluded_cities) != -1) {
      alert("we currently do not hire from the state of California.");
    }
  });
});

Open in new window


The blur event will be triggered when the user loses the focus of the search_location input, and it will check if the value entered matches one of the values of excluded_cities , so if yes it will alert the message else it will continue normally.
Hi Zakaria,

Thanks for the snippet.  I did try this,  put the code in a file /wp-content/themes/enfold/js/cali.js

But it's not doing anything when i type in cali etc.
You're welcome sir,

You need to make sure the code is accessible, clear the cache of your browser and try to add an alert just before the event to make sure it will be shown when the page is fully loaded.
Hi Zak, ok it's close. when I type in ai get the alert, but if after I've clicked ok, and then select a freight type, it will bring up the listings.  Is it possible to block selecting a fright type as well if California criteria are entered?
ASKER CERTIFIED SOLUTION
Avatar of Zakaria Acharki
Zakaria Acharki
Flag of Morocco 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
Hey Zak,   Almost there,  getting a few errors, wonded if you could help.

So i modified your code, so it takes out the checkboxes.  It's working in my chrome, but other browsers i'm getting

undefined is not a function (near '...$...')

Open in new window

<script>
	$(function() {
  $('body').on('blur', '#search_location', function() {
    var excluded_cities = ['ca', 'cali', 'california'];

    if ($.inArray($(this).val().toLowerCase(), excluded_cities) != -1) {
      alert("we currently do not hire from the state of California.");
      $('#job_type_dry-van').attr('disabled', 'disabled');
      $('#job_type_refrigerated').attr('disabled', 'disabled');
      $('#job_type_flat-bed').attr('disabled', 'disabled');
    }else{
      $('#job_type_dry-van').removeAttr('disabled', 'disabled');
      $('#job_type_refrigerated').removeAttr('disabled', 'disabled');
      $('#job_type_flat-bed').removeAttr('disabled', 'disabled');
    }
  });
});
	</script>

Open in new window

Hi,

I would not use that method, to my opinion this is not user friendly, plus any user can disable the popup by clicking the checkbox that will prevent to show it again..

Also let say you want to block other states in future this will become a huge JS for nothing and JS can easily bypass by any browser console..

This take a second to remove the disable attribute...

Instead why don't you display a select drop down list with only the states that are allowed?
and have a different field for city based on the state and another one for zip code

This will also shorten your MySQL query and better performance.

PS I do work on a trucking apps too :)
undefined is not a function (near '...$...')

Hi Jon, Please show me the full error, you could take a screenshot that may be helpful to detect the source of the error.