Hi there,
The javascript below checks whether the value entered by the user is in an array. It works fine, but I would like it to work with more than one array and more than one textbox. So I would like the javascript to check that the value entered into textbox A is in array A and that the value in textbox B is in array B. Can anyone suggest how I might achieve this?
I will probably need to expand the form in future, so I will need to check many arrays associated with many textboxes. Ideally, therefore, I would like one function that is easy to expand and that hopefully brings back one alert message listing all the things not in each array.
Many thanks,
Katie
The javascript below checks whether the value entered by the user is in an array. It works fine, but I would like it to work with more than one array and more than one textbox. So I would like the javascript to check that the value entered into textbox A is in array A and that the value in textbox B is in array B. Can anyone suggest how I might achieve this?
Code:
<html>
<head>
</head>
<body>
<script language=javascript>
Array.prototype.inArray = function (value)
{
// Returns true if the passed value is found in the
// array. Returns false if it is not.
var i;
for (i=0; i < this.length; i++)
{
if (this[i] == value)
{
return true;
}
}
return false;
};
function cal(xx)
{
var arr= new Array(1,2,3,4,5,6,7,8,9,10);
if(arr.inArray(xx))
{
alert("Your value is found in the Array");
}
else
{
alert("Your value is not found in the Array");
}
}
</script>
<div>
Enter value into Textbox to check whether it is present in array.
The array has value from 1 to 10.
<form name=form1 onSubmit="cal(form1.val.value)">
Value: <input type=text name=val size=12>
<input type=submit name=cs value="Submit">
</form>
</div>
</body>
</html>
I will probably need to expand the form in future, so I will need to check many arrays associated with many textboxes. Ideally, therefore, I would like one function that is easy to expand and that hopefully brings back one alert message listing all the things not in each array.
Many thanks,
Katie