2017-08-01 52 views
1

我有一個HTML輸入字段,我需要JavaScript來檢查輸入到此框中的輸入是否是某個字符串。具體來說,它必須是一個特定的郵政編碼,總共有9個不同的郵政編碼,所有這些都是不同的,並且沒有數字順序。一旦代碼檢查它是否是特定的郵政編碼,它將返回「是」,如果不是,則返回「是」。Javascript的檢查,如果輸入是一個特定的字符串

我知道如何使用ints完成此操作,如下面的代碼所示,但不知道如何使用字符串執行此操作。這是我當前的代碼,它與驗證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>

+0

使用'parseInt'爲'.value'將返回它作爲一個字符串,即使它的一些 –

+0

店的有效值在一個數組並檢查輸入值是否在數組中 – charlietfl

+0

如果你想支持zip + 4代碼,你必須用字符串而不是整數。 – James

回答

4

我認爲你是在思維這一點。您可以使用indexOf函數來測試您的郵政編碼數組。

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>

+0

這個工作,除了他們是郵政編碼,就像其中之一是「E1」。當我輸入「E1」時,它起作用,但「e1」不起作用。我該如何做到這一點,所以無論大小寫都無關緊要,無論價值是否被驗證? – user3371494

+0

@ user3371494要將歌曲視爲不區分大小寫,只需在要比較的字符串上使用'.toLowerCase()'方法,並確保數組中存儲的字符串包含較小的外殼值。我會更新答案來展示這一點。 –

+0

真棒,非常感謝。如果你能幫助我,我還有一件事。如果輸入任何這些郵政編碼,我都會顯示一個聯繫表單。我將如何去隱藏,直到其中一個郵政編碼被擊中? – user3371494

0

您可以使用郵政編碼正則表達式?請注意,這是一組以字符串格式表示的郵政編碼,但歡迎您創建一個郵政編碼正則表達式,以滿足您感興趣的一組郵政編碼。此外,如果該集合足夠小,您可以可能只是枚舉它們在列表/集合中,並檢查集合是否包含輸入。

<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>

0

其轉換爲數字

x = Number(document.getElementById("numb").value); 
相關問題