I created a function wherein if a textField has no value/input, it will prompt an alert.
However, the function is not working. Things I consider that makes it not work:
1) the "inp" variable
2) declaration at <form> tag
3) looping the all the textfields

What do you think? Please help. Thanks in advance.

<html>
	<head>
		<title>JavaScript</title>
		<style type="text/css">
			font{
				color:#663300;
				font-variant:small-caps;
				font-family:calibri;
				font-size:15;
			}
		</style>
		<script type="text/javascript">
			function Assess(){
			var g ;
			for (x=0;x<inp;x++){
				g = document.formKo.BoxText[x].value;
				if (g==""){
					alert("No input found at TextField # ." + g);
				}
			}
		}
		</script>
	</head>
	<body>
		<font>
		<form id="formKo" action="JavaScript:Assess()">
		<script type="text/javascript">
			var n = '\n';
			var inp;
			
			do{
				inp = prompt("Number of inputs to assess." + n + n + "Maximum of 10.");//asks user how many input will the program assess
			}while (inp > 10 || inp <=0);
			var txt = "<input type='text' name='BoxText'/><br/><br/>";
			for (i=1;i<=inp;i++){
				document.write("TextField # " + i + " " + txt );//creates textfields depending upon the "inp" variable
			}
		</script>
		<input type="submit" name="submit" value="Submit"/>
		<input type="reset" name="reset" value="Reset"/>
		</form>
		</font>
	</body>
</html>

Recommended Answers

All 6 Replies

<html>
	<head>
		<title>JavaScript</title>
		<style type="text/css">
			font{
				color:#663300;
				font-variant:small-caps;
				font-family:calibri;
				font-size:15;
			}
		</style>
		<script type="text/javascript">
                <!--
			function Assess(){
			var g ;
//			alert(document.all.formKo.BoxText);
			for (x=0;x<inp;x++){
				g = document.formKo.BoxText[x].value;
				if (g==""){
					alert("No input found at TextField #"+(x+1)+" ." + g);
				}
			}
		} 
                //-->
		</script>
	</head>
	<body>
		<form name="formKo" action="JavaScript:Assess()">
		<script type="text/javascript">
                <!--
			var n = '\n';
			var inp;
			
			do{
				inp = prompt("Number of inputs to assess." + n + n + "Maximum of 10.");//asks user how many input will the program assess
			}while (inp > 10 || inp <=0);
			var txt = "<input type='text' name='BoxText'/><br/><br/>";
			for (i=1;i<=inp;i++){
				document.write("<font>TextField # " + i + "</font> " + txt );//creates textfields depending upon the "inp" variable
			}
                //-->
		</script>
		<input type="submit" name="submit" value="Submit"/>
		<input type="reset" name="reset" value="Reset"/>
		</form>
	</body>
</html>

problem was..
1. form has no name.
if you wanna get document.formKo.. you must use name attribute formKo
or document.all.formKo on explorer(only)..
or document.forms[0]... with no id or name.

2. use font tag on text. it makes no error while runtime.. but DTD show you error on it~;)

Another suggestion is that when you accept an input from user and want to use it as an integer, ensure to convert it to integer using parseInt(value, base) before you use it. In this case 'inp' should be converted as

inp = parseInt(inp, 10)

before you use it; otherwise, user can enter any integer immediately followed by a string will pass the test but won't pass the loop. (Try 5a as the value and you will get no text fields but buttons.)

still not working...

What not working? It works on mine (at least on FF) using his code. What browser are you using? IE?

now working. thanks to asmira.

asmira, please reply to this thread so you'll get the points.

I`m here~;)
I`m glad to you solved it.(Is it right? i`m sorry that i`m not good at english ;) )

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.