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!

onSubmit

Status
Not open for further replies.

WannaLearn

Programmer
Jul 10, 2001
210
0
0
US
How can I add two function in the <form> tage? I tried this, and it does not work right.
<form name=&quot;form1&quot; action=&quot;&quot; method=&quot;post&quot; onSubmit=&quot;return checking(),checkchars(this);&quot;> Is there anything wrong with this statement?
 
I don't think jlsmithprism's solution will work.

Try :

<form name=&quot;form1&quot; action=&quot;&quot; method=&quot;post&quot; onSubmit=&quot;return (checking() && checkchars(this))&quot;>

That way it will check that checking() is true, and checkchars(this) returns true as well.

Hope this helps. Gary Haran
 
Hi Xutopia, I tried that it doesn't do anything.
This is my script.
Code:
<html>
<head>
<title>Discussion - Start a New Thread</title>

<SCRIPT LANGUAGE=&quot;JavaScript&quot; SRC=&quot;profane_script.js&quot;></SCRIPT>
<script>
function checking()  {
  if(document.form1.subject.value.length < 2) {
    alert(&quot;Please enter a TOPIC-SUBJECT heading that is more than 2 characters.&quot;);
    document.form1.subject.value = &quot;&quot;;
    document.form1.subject.focus();
    return false;
  }
  if(document.form1.bd.value.length < 2) {
    alert(&quot;Please enter your Comment.&quot;);
    document.form1.bd.value = &quot;&quot;;
    document.form1.bd.focus();
    return false;
  }
}
</script>
<script>
<!--
if(navigator.appName == &quot;Netscape&quot;) {
 function checkchars(cur) {
  var maxlength=10
    if (document.form1.bd.value.length>maxlength){
      alert(&quot;Please restrain your input to 10 or less characters!&quot;)
    return false
    }
 }
}
if(navigator.appName == &quot;Internet Explorer&quot;) {
 function taLimit() {
  var taObj=event.srcElement;
  if (taObj.value.length==taObj.maxLength*1) return false;
 }

 function taCount(visCnt) { 
  var taObj=event.srcElement;
  if (taObj.value.length>taObj.maxLength*1) taObj.value=taObj.value.substring(0,taObj.maxLength*1);
  if (visCnt) visCnt.innerText=taObj.maxLength-taObj.value.length;
 }
}
//-->
</script>
</head>
<body background=&quot;&quot;>
<br>
<br>
<TABLE border=&quot;0&quot; align=&quot;center&quot; width=&quot;540&quot;>
  <TR> 
    <TD align=&quot;left&quot; valign=&quot;top&quot; width=&quot;366&quot; rowspan=&quot;2&quot;> 
      <TABLE width=&quot;362&quot;>
        <form name=&quot;form1&quot; action=&quot;&quot; method=&quot;post&quot; onSubmit=&quot;return (checking() && checkchars(this))&quot;>
          <TR> 
            <TD width=&quot;354&quot;><font face=&quot;Arial, Tahoma, Verdana&quot; size=&quot;2&quot; color=&quot;f00000&quot;><b>Topic:</b></font></TD>
          </TR>
          <TR> 
            <TD width=&quot;354&quot;> 
              <Input type=&quot;Text&quot; name=&quot;subject&quot; size=&quot;40&quot; onBlur = &quot;checkForProfanity(0);&quot;>
            </TD>
          </TR>
          <TR> 
            <TD width=&quot;354&quot;><b><font face=&quot;Arial, Tahoma, Verdana&quot; size=&quot;2&quot; color=&quot;f00000&quot;>Comment(s):</font></b></TD>
          </TR>
          <TR> 
            <TD width=&quot;354&quot;> 
              <textarea name=&quot;bd&quot; onkeypress=&quot;return taLimit()&quot; onkeyup=&quot;return taCount(myCounter)&quot; cols=&quot;40&quot; rows=&quot;10&quot; wrap=physical maxLength=&quot;10&quot; onBlur = &quot;checkForProfanity(1);&quot;></textarea>
              <br>
              <font face=&quot;Arial, Tahoma, Verdana&quot; size=&quot;2&quot; color=&quot;004080&quot;>Maximum 
              characters alloted is 10.</font> </TD>
          </TR>
          <TR> 
            <TD width=&quot;354&quot;> 
              <input type=&quot;Submit&quot; name=&quot;Submit&quot; value=&quot;Submit Comments&quot;>
              <input type=&quot;Reset&quot; name=&quot;Reset&quot; value=&quot;Reset&quot;>
            </TD>
          </TR>
        </FORM>
      </TABLE>
    </td>
  </tr>
  <TR> </tr>
</table>
<br>
</BODY>
</HTML>
 
The problem is in NS4, if i input more the the alloted characters no alert message comes.
 
Sorry I made a mistake, this is the script. The script above had other errors that I fixed, but this script works good in IE in NS4 however, I get errors.
Can anyone help?

Code:
<html>
<head>
<title>Discussion - Start a New Thread</title>

<SCRIPT LANGUAGE=&quot;JavaScript&quot; SRC=&quot;profane_script.js&quot;></SCRIPT>
<script>
function checking()  {
  if(document.form1.subject.value.length < 2) {
    alert(&quot;Please enter a TOPIC-SUBJECT heading that is more than 2 characters.&quot;);
    document.form1.subject.value = &quot;&quot;;
    document.form1.subject.focus();
    return false;
  }
  if(document.form1.bd.value.length < 2) {
    alert(&quot;Please enter your Comment.&quot;);
    document.form1.bd.value = &quot;&quot;;
    document.form1.bd.focus();
    return false;
  }
}
</script>
<script>
<!---
if(navigator.appName == &quot;Netscape&quot;) {
 function checkchars(cur) {
  var maxlength=10
    if (cur.bd.value.length > maxlength) {
      alert(&quot;Please restrain your input to 10 or less characters!&quot;);
	  document.form1.bd.focus();
      return false;
    }
 }
}

if(navigator.appName == &quot;Internet Explorer&quot;) {
 function taLimit() {
  var taObj=event.srcElement;
  if (taObj.value.length==taObj.maxLength*1) return false;
 }
}
//--->
</script>
</head>
<body>
<br>
<br>
<TABLE border=&quot;0&quot; align=&quot;center&quot; width=&quot;540&quot;>
  <TR> 
    <TD align=&quot;left&quot; valign=&quot;top&quot; width=&quot;366&quot; rowspan=&quot;2&quot;> 
      <TABLE width=&quot;362&quot;>
        <form name=&quot;form1&quot; action=&quot;&quot; method=&quot;post&quot; onsubmit=&quot;return (checking() && checkchars(this))&quot;>
          <TR> 
            <TD width=&quot;354&quot;><font face=&quot;Arial, Tahoma, Verdana&quot; size=&quot;2&quot; color=&quot;f00000&quot;><b>Topic:</b></font></TD>
          </TR>
          <TR> 
            <TD width=&quot;354&quot;> 
              <Input type=&quot;Text&quot; name=&quot;subject&quot; size=&quot;40&quot; onBlur = &quot;checkForProfanity(0);&quot;>
            </TD>
          </TR>
          <TR> 
            <TD width=&quot;354&quot;><b><font face=&quot;Arial, Tahoma, Verdana&quot; size=&quot;2&quot; color=&quot;f00000&quot;>Comment(s):</font></b></TD>
          </TR>
          <TR> 
            <TD width=&quot;354&quot;> 
              <textarea name=&quot;bd&quot; onkeypress=&quot;return taLimit()&quot; cols=&quot;40&quot; rows=&quot;10&quot; wrap=physical maxLength=&quot;10&quot; onBlur=&quot;checkForProfanity(1);&quot;></textarea>
              <br>
              <font face=&quot;Arial, Tahoma, Verdana&quot; size=&quot;2&quot; color=&quot;004080&quot;>Maximum 
              characters alloted is 10.</font> </TD>
          </TR>
          <TR> 
            <TD width=&quot;354&quot;> 
              <input type=&quot;Submit&quot; name=&quot;Submit&quot; value=&quot;Submit Comments&quot;>
              <input type=&quot;Reset&quot; name=&quot;Reset&quot; value=&quot;Reset&quot;>
            </TD>
          </TR>
        </FORM>
      </TABLE>
    </td>
  </tr>
  <TR> </tr>
</table>
<br>
</BODY>
</HTML>
 
lets do some cleaning up first :)

Having function exist only if it is a certain browser is rather unconventional. Could you find a better way of doing this ?

for example :

if(navigator.appName == &quot;Internet Explorer&quot;) {
function taLimit() {
var taObj=event.srcElement;
if (taObj.value.length==taObj.maxLength*1) return false;
}
}

Could be :

function taLimit() {
if (window.event)
{

var taObj = event.srcElement;
if (taObj.value.length==taObj.maxLength*1) return false;
}
else
{
// other code here
}
}

Do you see that I check not on what browser you have but what things exist in that browser? Gary Haran
 
Xutoipa, once again that method works but in IE, in NS it just ignores it completely. I hate NS so much now!
This is how I wrote the script:

<script>
<!---
function taLimit() {
if (window.event) {
var taObj = event.srcElement;
if (taObj.value.length==taObj.maxLength*1) return false;
}
else {
var maxlength=10;
if (cur.bd.value.length > maxlength) {
alert(&quot;Please restrain your input to 10 or less characters!&quot;);
return false;
}
}
}
//--->
</script>

This is the <form name=&quot;form1&quot; action=&quot;&quot; method=&quot;post&quot; onSubmit=&quot;return checking();&quot;>
And this is the textarea: <textarea name=&quot;bd&quot; onkeypress=&quot;return taLimit()&quot; cols=&quot;40&quot; rows=&quot;10&quot; wrap=physical maxLength=&quot;10&quot; onBlur=&quot;checkForProfanity(1);&quot;>
 
Take a look at this. This works in both IE 5.5 and Netscape 4.7. The function works in both. You just need to call it from the onKeyPress if that's how you want it to work.

<html>
<head>
<title>Discussion - Start a New Thread</title>
<script>

function checkTextAreaLength(oTextArea, maxLength)
{
if( oTextArea.value.length > parseInt(maxLength) )
{
alert(&quot;You entered too many characters!&quot;);
return false;
}

return true;
}

</script>
</head>
<body>
<br>
<br>
<form name=&quot;form1&quot; onsubmit=&quot;return checkTextAreaLength(document.form1.bd,10)&quot;>
<input type=&quot;text&quot; name=&quot;subject&quot; size=&quot;40&quot;>
<br>
<textarea name=&quot;bd&quot; cols=&quot;40&quot; rows=&quot;10&quot; wrap=&quot;physical&quot; maxlength=&quot;10&quot;></textarea>
<br>
<input type=&quot;Submit&quot; name=&quot;Submit&quot; value=&quot;Submit Comments&quot;>
<input type=&quot;Reset&quot; name=&quot;Reset&quot; value=&quot;Reset&quot;>
</form>
</body>
</html>
 
Ok, let me try that out now.
But I tried something else also. I know this works fine in both IE and NS, but when I implement it to my use it doesn't work. I think the problem is calling of both functions, in the <form>
Code:
<html>
<head>
<title>Discussion - Start a New Thread</title>

<SCRIPT LANGUAGE=&quot;JavaScript&quot; SRC=&quot;profane_script.js&quot;></SCRIPT>
<script>
function checking()  {
  if(document.form1.subject.value.length < 2) {
    alert(&quot;Please enter a TOPIC-SUBJECT heading that is more than 2 characters.&quot;);
    document.form1.subject.value = &quot;&quot;;
    document.form1.subject.focus();
    return false;
  }
  if(document.form1.chars.value.length < 2) {
    alert(&quot;Please enter your Comment.&quot;);
    document.form1.chars.value = &quot;&quot;;
    document.form1.chars.focus();
    return false;
  }
}
</script>
<script>
function checkchars(cur){
//change max length to determine below
var maxlength=10
if (cur.chars.value.length>maxlength){
alert(&quot;Please restrain your input to 10 or less characters!&quot;)
return false
}
}
</script>
</head>
<body>
<br>
<br>
<TABLE border=&quot;0&quot; align=&quot;center&quot; width=&quot;540&quot;>
  <TR> 
    <TD align=&quot;left&quot; valign=&quot;top&quot; width=&quot;366&quot; rowspan=&quot;2&quot;> 
      <TABLE width=&quot;362&quot;>
        <form name=&quot;form1&quot; action=&quot;&quot; method=&quot;post&quot; onSubmit=&quot;return (checking() && checkchars(this))&quot;>
          <TR> 
            <TD width=&quot;354&quot;><font face=&quot;Arial, Tahoma, Verdana&quot; size=&quot;2&quot; color=&quot;f00000&quot;><b>Topic:</b></font></TD>
          </TR>
          <TR> 
            <TD width=&quot;354&quot;> 
              <Input type=&quot;Text&quot; name=&quot;subject&quot; size=&quot;40&quot; onBlur = &quot;checkForProfanity(0);&quot;>
            </TD>
          </TR>
          <TR> 
            <TD width=&quot;354&quot;><b><font face=&quot;Arial, Tahoma, Verdana&quot; size=&quot;2&quot; color=&quot;f00000&quot;>Comment(s):</font></b></TD>
          </TR>
          <TR> 
            <TD width=&quot;354&quot;> 
              <textarea cols=&quot;40&quot; rows=&quot;10&quot; name=&quot;chars&quot; wrap=&quot;physical&quot;  onBlur=&quot;checkForProfanity(1);&quot;></textarea>
              <br>
              <font face=&quot;Arial, Tahoma, Verdana&quot; size=&quot;2&quot; color=&quot;004080&quot;>Maximum 
              characters alloted is 10.</font> </TD>
          </TR>
          <TR> 
            <TD width=&quot;354&quot;> 
              <input type=&quot;Submit&quot; name=&quot;Submit&quot; value=&quot;Submit Comments&quot;>
              <input type=&quot;Reset&quot; name=&quot;Reset&quot; value=&quot;Reset&quot;>
            </TD>
          </TR>
        </FORM>
      </TABLE>
    </td>
  </tr>
  <TR> </tr>
</table>
<br>
</BODY>
</HTML>
 
When I want to make more then one function call from onSubmit, I like to call a function that acts as my handler. So rather than deal with the logic you have for two function calls, just create a function called submitHandler() and do all the logic you want in it. Then you have one return value coming back to the onSubmit. Although this is not necessary, I think it adds to the readability of your program.
 
Wannalearn, try this:
Code:
<html>
<head>
<title>Discussion - Start a New Thread</title>

<SCRIPT LANGUAGE=&quot;JavaScript&quot; SRC=&quot;profane_script.js&quot;></SCRIPT>
<script>
function checking()  {
  if(document.form1.subject.value.length < 2) {
    alert(&quot;Please enter a TOPIC-SUBJECT heading that is more than 2 characters.&quot;);
    document.form1.subject.value = &quot;&quot;;
    document.form1.subject.focus();
    return false;
  }
  if(document.form1.bd.value.length < 2) {
    alert(&quot;Please enter your Comment.&quot;);
    document.form1.bd.value = &quot;&quot;;
    document.form1.bd.focus();
    return false;
  }
  if(document.form1.bd.value.length > 10) {
    alert(&quot;Please restrain your input to 10 or less characters!&quot;);
	document.form1.bd.focus();
    return false;
  }
}
</script>
<script>
<!---
function taLimit() {
  var taObj=event.srcElement;
  if (taObj.value.length==taObj.maxLength*1) return false;
}

function taCount(visCnt) { 
  var taObj=event.srcElement;
  if (taObj.value.length>taObj.maxLength*1) taObj.value=taObj.value.substring(0,taObj.maxLength*1);
  if (visCnt) visCnt.innerText=taObj.maxLength-taObj.value.length;
}
//--->
</script>
</head>
<body>
<br>
<br>
<TABLE border=&quot;0&quot; align=&quot;center&quot; width=&quot;577&quot;>
  <TR> 
    <TD align=&quot;left&quot; valign=&quot;top&quot; width=&quot;422&quot;> 
      <TABLE width=&quot;384&quot;>
        <form name=&quot;form1&quot; action=&quot;newthreadp.cfm&quot; method=&quot;post&quot; onSubmit=&quot;return checking();&quot;>
          <TR> 
            <TD width=&quot;354&quot;><font face=&quot;Arial, Tahoma, Verdana&quot; size=&quot;2&quot; color=&quot;f00000&quot;><b>Topic:</b></font></TD>
          </TR>
          <TR> 
            <TD width=&quot;354&quot;> 
              <Input type=&quot;Text&quot; name=&quot;subject&quot; size=&quot;40&quot; onBlur = &quot;checkForProfanity(0);&quot;>
            </TD>
          </TR>
          <TR> 
            <TD width=&quot;354&quot;><b><font face=&quot;Arial, Tahoma, Verdana&quot; size=&quot;2&quot; color=&quot;f00000&quot;>Comment(s):</font></b></TD>
          </TR>
          <TR> 
            <TD width=&quot;354&quot;> 
              <textarea name=&quot;bd&quot; onkeypress=&quot;return taLimit()&quot; onkeyup=&quot;return taCount(myCounter)&quot; cols=&quot;40&quot; rows=&quot;10&quot; wrap=physical maxLength=&quot;10&quot; onBlur = &quot;checkForProfanity(1);&quot;></textarea>
              <br>
              <font face=&quot;Arial, Tahoma, Verdana&quot; size=&quot;2&quot; color=&quot;004080&quot;>You 
              have <B><SPAN id=myCounter>10</SPAN></B> characters remaining for 
              your description...</font> </TD>
          </TR>
          <TR> 
            <TD width=&quot;354&quot;> 
              <input type=&quot;Submit&quot; name=&quot;Submit&quot; value=&quot;Submit Comments&quot;>
              <input type=&quot;Reset&quot; name=&quot;Reset&quot; value=&quot;Reset&quot;>
            </TD>
          </TR>
        </FORM>
      </TABLE>
    </td>
  </tr>
</table>
<br>
</BODY>
</HTML>
I had to change some element names and such, but it works in both NS and IE... I have not failed; I merely found 100,000 different ways of not succeding...
 
GUJUm0deL, thank you so much! Exactly what I needed!
To everyone else, xutopia and joeoz, thank-you so much also. :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top