Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Validation to check that value entered is in array

Status
Not open for further replies.

Katie6

Programmer
Jun 12, 2007
58
GB
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?

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
 
Well, I'd modify that function you've already made to accet two parameters: an array and a value so you can reuse it.

Cheers,
Dian
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top