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

Netscape Form Validation

Status
Not open for further replies.

ded

Programmer
Nov 3, 2000
41
GB
The following piece of javascript does work as I would expect in Netscape. What happens it that when you click check without typing anything it will display the message as expected but then if you type something and click then it still displays the message which it shouldn't. If you click again it is fine.

Any ideas?

Duncan

Code:
<html>
<head>
<title></title>
<script language=&quot;JavaScript&quot;>
<!--
function checkfortext() {
 if (!TextAreaRequired('weaknesses','sform2','textfield')) {
 alert(&quot;You must enter some weaknesses.&quot;);
 return false;
 }
 //call to another function
}

function TextAreaRequired(Layer, Form, Field) {
 if (navigator.appVersion.indexOf(&quot;MSIE&quot;) > 0) {
 	var length = eval(&quot;document.&quot;+ Form +&quot;.&quot; + Field +&quot;.value.length&quot;);
 } else {
 	var length = eval(&quot;document.&quot;+Layer+&quot;.document.&quot;+ Form +&quot;.&quot; + Field +&quot;.value.length&quot;);
 }

 if (length == 0) {
 	return false;
 } else {
 	return true;
 }
}
//-->
</script>
</head>
<body>
<div id=&quot;weaknesses&quot; style=&quot;position:absolute; left:291px; top:295px; width:218px; height:143px; z-index:7&quot;>
  <b><font face=&quot;Verdana, Arial, Helvetica, sans-serif&quot; size=&quot;2&quot; color=&quot;#FFFFFF&quot;>Weaknesses</font></b>
  <form name=&quot;sform2&quot; method=&quot;post&quot; action=&quot;&quot;>
    <textarea name=&quot;textfield&quot; cols=&quot;12&quot; rows=&quot;5&quot;></textarea>
    <a href=&quot;#&quot; onMouseDown=&quot;checkfortext()&quot;>Check</a>
  </form>
</div>
</body>
</html>
 
it'll work better in both browser if you suppress
<a href=&quot;#&quot; onMouseDown=&quot;checkfortext()&quot;>Check</a>
and replace it with
<form name=&quot;sform2&quot; method=&quot;post&quot; action=&quot;&quot; onsubmit=&quot;checkfortext()&quot;>
and don't forget to add
<input type=submit value=&quot;Check&quot;>


 
Try this:

Code:
<html>
<head>
<title></title>
<script language=&quot;JavaScript&quot;>
<!--
function checktext() {

	n = (document.all) ? 0 : 1; // check for netscape
	if(n) {
		var len = document.forms[0].elements[0].value.length;
	} else {
		var len = document.myform.textfield.value.length;
	}

	if(len) {
		// Call to another function
	} else {
		alert(&quot;Please enter some text&quot;);
	}

	return;

}
//-->
</script>
</head>
<body>
<div id=&quot;weaknesses&quot;>
<b><font face=&quot;Verdana, Arial, Helvetica, sans-serif&quot; size=&quot;2&quot; color=&quot;#FFFFFF&quot;>Weaknesses</font></b>
  <form name=&quot;myform&quot; method=&quot;post&quot; action=&quot;&quot;>
    <textarea name=&quot;textfield&quot; cols=&quot;12&quot; rows=&quot;5&quot;></textarea>
    <input type=button name=&quot;check&quot; value=&quot;check&quot; onClick=&quot;checktext();&quot;>
  </form>
</div>
</body>
</html>

Don't ask me why but if you inlude the STYLE tag in the <DIV> in Netscape it can't find the form element. Try using a stylesheet instead to overcome this.

Hope this helps,

Tim
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top