1

I've read through many posts, and tried various options, but my validation check on my JavaScript doesn't seem to be running.

I used this post to try and write a validation check to see if the entered zip code is one one delivered to, but when I run it nothing happens and neither alert shows up. Any help is appreciated.

This is the JavaScript I was using:

    <script type="text/javascript">
function validateZip() {
  var zipCode = $("#zipCode").val();
  var acceptableZipCodes = ["78205","72215","78212",];

if( $.inArray( zipCode, acceptableZipCodes)){
  alert("Yes, we can help!");
    }else{
  alert("Sorry, we don't deliever to your area yet.");
}
}
</script>

This is the form

<form name="zipForm" onsubmit="return(validateForm(zipCode))">
  <input type="text" id="zipCode" name="zipCode"> 
    <input id="user_info" type="submit">
</form>
2

4 Answers 4

1
function validateZip() {
  var zipCode = document.zipForm.zipCode.value;
  var acceptableZipCodes = ["78205","72215","78212",];

  for (i = 0; i < cars.acceptableZipCodes.length; i++) { 
    if(acceptableZipCodes[i] == zipCode{
     alert("Yes, we can help!");
    }else{
     alert("Sorry, we don't deliever to your area yet.");
    }
  }
}
Sign up to request clarification or add additional context in comments.

Comments

1

There are 2 mistakes I found. 1, you define the function validateZip, but you didn't call it in the script. 2, inArray() function will be return the position of the value in the array, so when a value doesnt in the array, it will return -1.

I do some changes in the code. Hope it help you.

<form name="zipForm" onsubmit="return(validateZip())">
  <input type="text" id="zipCode" name="zipCode"> 
    <input id="user_info" type="submit">
</form>

<script type="text/javascript">
function validateZip() {
  var zipCode = $("#zipCode").val();
  var acceptableZipCodes = ["78205","72215","78212",];

if( $.inArray( zipCode, acceptableZipCodes) !== -1){
  alert("Yes, we can help!");
    }else{
  alert("Sorry, we don't deliever to your area yet.");
    }
}
</script>

1 Comment

Thank you, this is exactly what I needed!
1

I would use an ID instead of name attribute for your form tag (for a quicker retrieval).

<form id="zipForm">
   <input type="text" id="zipCode" name="zipCode"> 
   <input id="user_info" type="submit">
</form>

Then below, I would do this:

<script>
$(function() {
  // add an event handler to your form (look at adeneo's comment)
  $('#zipForm').on('submit', function(e) {
      // prevent form from submitting
      e.preventDefault();
      // call your function
      validateZip($("#zipCode").val());
  });

  function validateZip(zipCode) {
    var acceptableZipCodes = ["78205", "72215", "78212"];
    // be careful with using $.inArray as the return value for no 
    // matches is -1 (and not false)
    if ($.inArray(zipCode, acceptableZipCodes) != -1) {
      alert("Yes, we can help!");
    } else {
      alert("Sorry, we don't deliever to your area yet.");
    }
  }
});
</script>

Comments

0

You should check if it's begger than -1:

if($.inArray( zipCode, acceptableZipCodes) > -1)

Here a jsfiddle example: JSFiddle

1 Comment

Thanks! I'll go in and add that as well

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.