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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Alert if input is not in array, except if input is blank-not working!

Status
Not open for further replies.

Katie6

Programmer
Jun 12, 2007
58
0
0
GB
Hi there,
The following code brings up an alert message if the user enters something into a textbox that isn't in an array. It works fine, but I do not want the alert message to appear if a box is blank, i.e. blank is an acceptable input.

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
   "[URL unfurl="true"]http://www.w3.org/TR/html4/strict.dtd">[/URL]
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<style type="text/css">
li {
    margin-bottom:5px;
 }
</style>

<script type="text/javascript">
   var arr=[
            [1,2,3,4,5],
            ['a','b','c','d','e'],
			['a1','b1','c1','d1,','e1'],
            ['dog','cat','mouse','budgie','fish']
           ];
   var inps;
   var count=[0,0,0,0]; /*Note that the count length must be equal to arr length*/
	window.onload=function() {
   inps=document.forms[0].getElementsByTagName('input');
	document.forms[0].onsubmit=function() {
   return checkArrays();
 }
}
	function checkArrays() {
	
	var alertMessage = "The following value/s are not found:\n\n";
	
		for(var k=0;k<inps.length;k++) {
			if(inps[k].id=='inp'+k){
				for(var c=0;c<arr[k].length;c++){
					if(inps[k].value.match(arr[k][c])){
					count[k]=1;
					alertMessage = alertMessage;
					}
				}
			}
		}
		
		for(var m=0;m<arr.length;m++) {
			if(count[m]==0){
				alertMessage += ''+inps[m].value+'\n\n'
				document.forms[0][m].value='';
				document.forms[0][m].focus();
			}
		}
		if (alertMessage != "The following value/s are not found:\n\n") 
			{
				alert(alertMessage);
				return (false);
			}

   return true;
 }

</script>

</head>
<body>

<form action="[URL unfurl="true"]http://www.google.com/"[/URL] method="get">
<ol>
<li><input id="inp0" name="number" type="text" value=""><label> : numbers</label></li>
<li><input id="inp1" name="letter" type="text" value=""><label> : letters</label></li>
<li><input id="inp2" name="letNum" type="text" value=""><label> : letter and number</label></li>
<li><input id="inp3" name="animal" type="text" value=""><label> : animal</label></li>
<li><input type="submit"></li>
</ol>
</form>

</body>
</html>

In summary, I am trying to achieve the following, where unacceptable inputs trigger an alert message and acceptable inputs don't:

Acceptable input
- when the user enters something that is in an array
- when the user enters a blank

Unacceptable input
- when the user enters something that is not in the array, expect if that something is a blank.

It may be a very simple problem to solve, but I'm stumped! Can anyone help me?

Many thanks,

Katie
 
Did you write the code above? Checking for a blank value seems like a very trivial thing to do if you were able to write all of the code you posted.

Anyway....

Code:
        for(var k=0;k<inps.length;k++) {
            if(inps[k].id=='inp'+k){
                for(var c=0;c<arr[k].length;c++){
                    if(inps[k].value.match(arr[k][c]) [!]&& inps[k].value.length[/!]){
                    count[k]=1;
                    [gray]//is this line even needed?[/gray]
                    [s]alertMessage = alertMessage;[/s]
                    }
                }
            }
        }

-kaht

Lisa, if you don't like your job you don't strike. You just go in every day and do it really half-assed. That's the American way. - Homer Simpson

[small]<P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <.</B>[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top