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