1

I have an HTML Input field and I need javascript to check if the input entered into this box is a certain string. Specifically, it has to be a specific Zip code, there are a total of 9 different zip codes, all which are different and in no numerical order. Once the code checks if it is that specific zip code, it returns "Yes", if not, simply no.

I know how to do this with ints, as shown in the code below, but not sure to how to do this with strings. This is my current code, which works with validating an integer between 1-10:

<input id="numb">

<button type="button" onclick="myFunction()">Submit</button>

<p id="demo"></p>

<script>
function myFunction() {
    var x, text;

    // Get the value of the input field with id="numb"
    x = document.getElementById("numb").value;

    // If x is Not a Number or less than one or greater than 10
    if (isNaN(x) || x < 1 || x > 10) {
        text = "Input not valid";
    } else {
        text = "Input OK";
    }
    document.getElementById("demo").innerHTML = text;
}
</script>

3
  • use parseInt as .value will return it as a string even if its a number Commented Aug 1, 2017 at 20:47
  • store the valid values in an array and check if value entered is in the array Commented Aug 1, 2017 at 20:48
  • If you want to support zip + 4 codes you'll have to do something with strings instead of integers. Commented Aug 1, 2017 at 21:14

3 Answers 3

4

I think you are over-thinking this. You can just use the indexOf function to test your zip code array.

var btn= document.getElementById("btn");
var input = document.getElementById("numb");
var output = document.getElementById("demo"); 
var formArea = document.getElementById("formArea"); 

var zips = ["11111","22222","33333","44444","55555", "e1", "e2"];

btn.addEventListener("click", function() {
    var result = null;

    // indexOf() returns -1 when the supplied value isn't present
    if(zips.indexOf(numb.value.toLowerCase()) > -1){
      result =  "yes";
      
      // Show the form by removing the hidden class
      formArea.classList.remove("hidden");
    } else {
      result = "no";
      // Hide the form by adding the hidden class
      formArea.classList.add("hidden");      
    }
    output.textContent = result;

});
#formArea{
  border:2px double grey;
  width:50%;
  box-shadow:2px 2px 0 #303030;
  height:100px;
  padding:5px;
}

.hidden { 
  display:none; 
}
<input id="numb">

<button type="button" id="btn">Submit</button>

<p id="demo"></p>

<div id="formArea" class="hidden ">
  Your form goes here
</div>

Sign up to request clarification or add additional context in comments.

4 Comments

this works, except they're post codes so like one of them is "E1". When I enter "E1", it works, but "e1" doesn't work. How do I make this so it doesn't matter if upper or lowercase, the value is validated regardless?
@user3371494 To treat sings as case-insensitive, just use the .toLowerCase() method on the string being compared and make sure that the stored string in your array, contains lower cased values. I'll update the answer to show this.
awesome, greatly appreciated. I just had one more thing if you could help me with. I have a contact form that I want to appear if any of those postcodes are entered. How would I go about hiding that until one of the postcodes are hit?
@user3371494 Also simple enough. You just have the form set up ahead of time with a CSS class that hides it by default. Then, in the same section of the code where you determine a code match, you remove that class. I will again update the answer to show this. Don't forget to mark as "the" answer.
0

Can you use a regular expression for postal codes? Note this accounts for a set of zip codes that are in string format, but you are welcome to create a zip-code regex that can satisfy the set of zip codes you are interested in. And furthermore, if the set is small enough you can probably just enumerate them in a list/set and check if the set contains the input.

<input id="numb">

<button type="button" onclick="myFunction()">Submit</button>

<p id="demo"></p>

<script>
function myFunction() {
    var x, text;

    var isValidZip = /(^\d{5}$)|(^\d{5}-\d{4}$)/;

    // Get the value of the input field with id="numb"
    x = document.getElementById("numb").value;

    // If x is Not a Number or less than one or greater than 10
    if (!isValidZip.test(x)) {
        text = "Input not valid";
    } else {
        text = "Input OK";
    }
    document.getElementById("demo").innerHTML = text;
}
</script>

Comments

0

convert it to a number

x = Number(document.getElementById("numb").value);

Comments

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.