Issue
This my HTML Code
<html>
<body>
<h2>Validation Array</h2>
<input id="search" onblur="read()">
<label id="creat"></label>
</body>
</html>
And this my Javascript
var fruits = ["Banana", "Orange", "Apple", "Mango"];
var search = document.getElementById("search").value;
function read() {
if(search != fruits){
document.getElementById("creat").innerHTML = "Error";
} else {
document.getElementById("creat").innerHTML = "Valid";
}
}
i want to validation write "Valid" if value = in array. thanks
Solution
There are a couple problems, you need to get the search value within the function, otherwise it will be the first value no matter which changes are made. Then, you can use Array.includes to check if the value is in the array.
var fruits = ["Banana", "Orange", "Apple", "Mango"];
function read() {
var search = document.getElementById("search").value;
if(!fruits.includes(search)) {
document.getElementById("creat").innerHTML = "Error";
} else {
document.getElementById("creat").innerHTML = "Valid";
}
}
<html>
<body>
<h2>Validation Array</h2>
<input id="search" onblur="read()">
<label id="creat"></label>
</body>
</html>
Answered By - JBaczuk
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.