prevent keystrokes "Enter" and semi-colon in textarea

I need to prevent users from entering semi-colons or returns in a specific textarea field (there are reasons why I need to keep it a textarea field, can't change it to a regular text input). How can I adapt the validation code below to prevent users from entering semi-colons or returns in the "Keywords" textarea. If the user attempts to enter a semi-colon or a return, I'd like to alert him/her and then set focus to the "Keywords" text area. I'd like the alert to happen on the keystroke. If the user ignores the alert and tries to go to another field or submit anyway, I'd like the alert to re-appear. Here's what I want to modify.

<HEAD>
<LINK href="/wcSuggest/wcSuggest.css" rel="stylesheet" type="text/css">
<SCRIPT language="JavaScript">
function titleCase(sValue) {
   var sReturn = '';
   var sWord = '';
   var sParts = sValue.split(" ");

   for (i = 0; i < sParts.length; i++){
      if (i > 0) {
         sReturn += ' ';
      }
      if (sParts[i].length > 0) {
         sWord = sParts[i].substr(0);
         sReturn += sParts[i].charAt(0).toUpperCase() + sParts[i].substr(1);
      }
   }
   return sReturn;
}

function validateForm(fObj){
  var aFld = ["site_url"];  //  var aFld = ["site_url", "myField2", "myField3"];
  var aStar = ["fld1"];  //  var aStar = ["fld1", "fld2", "fld3"];
  for(i=0;i<aStar.length;i++){
    document.getElementById(aStar[i]).style.display = "none";
  }
  msg = "";
  for(i=0;i<aFld.length;i++){
    if(fObj[aFld[i]].value==""){
      document.getElementById(aStar[i]).style.display = "";
      msg += titleCase(aFld[i])+" is a required field.\n";
       alert(msg);
       document.wcSuggestion.site_url.focus();
     }
  }
  if(msg>""){
    return false;
  } else {
    return true;
  }
}
</SCRIPT>
<TITLE>Suggest a site...</TITLE>
</HEAD>
<BODY text="000000" bgcolor="EFEFEF" leftmargin="35" bottommargin="55" topmargin="25" marginwidth="0" marginheight="0" link="0000FF" alink="FF0000" vlink="0000FF">
<FORM method=get action="wcSuggestAction.pl" name="wcSuggestion" onSubmit="return validateForm(this);">
  <P>
  <TABLE border="0" cellspacing="0" cellpadding="0" width="403"top">
    <TR valign=top>
      <TD width="184" valign="bottom" height="111" class="label">Keywords
        (optional):<BR>
        [Use commas to separate multiple keywords]<BR>
        <BR>
      </TD>
      <TD width="219" valign="bottom" height="111" align="right">
        <TEXTAREA name="Keywords" wrap="Virtual" rows="4" cols="24"></TEXTAREA>
      </TD>
    </TR>
    <TR valign=top>
      <TD width="184" valign=middle height="56"></TD>
      <TD width="219" valign=middle height="56" align="right">
        <INPUT type="submit"  value="Submit Site" class="submitbutton" onMouseOver="this.style.cursor='hand';">
      </TD>
    </TR>
    <TR valign=top>
      <TD colspan="2" height="28"><FONT size=2 face="Verdana">Date: </FONT><FONT size=2 face="Verdana">
        Tuesday August 10, 2004 </FONT></TD>
    </TR>
  </TABLE>
</FORM>
</BODY>
</HTML>
GessWurkerAsked:
Who is Participating?
 
justinbilligConnect With a Mentor Commented:
you can totally prevent them from entering the key by doing this

<TEXTAREA name="Keywords" wrap="Virtual" rows="4" cols="24" onkeypress="return CancelInvalidKeys( );"></TEXTAREA>


function CancelInvalidKeys( )
{
      // Assume True
      var blnReturnValue = true;

      if( event.keyCode == 59 ) // semicolan
      {
            blnReturnValue = false;
      }

      if( event.keyCode == 13 ) // enter
      {
            blnReturnValue = false;
      }

      return blnReturnValue;
}


just a little modification will allow you to alert if you want
0
 
ZylochCommented:
Hi

First, change this line:
<TEXTAREA name="Keywords" wrap="Virtual" rows="4" cols="24"></TEXTAREA> to
<TEXTAREA name="Keywords" wrap="Virtual" rows="4" cols="24" onkeypress="validateText(event);"></TEXTAREA>

Then, make this function:

function validateText(e) {
   var the_key = (window.event) ? event.keyCode : e.which;
   if (the_key == 59 || the_key == 13) {
      return false;
   }
}

Regards,
Zyloch
0
 
ZylochCommented:
Ah, yes, for the alert, have this:

function validateText(e) {
   var the_key = (window.event) ? event.keyCode : e.which;
   if (the_key == 59 || the_key == 13) {
      alert("You cannot press semicolon or <enter>");
      document.forms["wcSuggestion"].Keywords.focus();
      return false;
   }
}
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

 
GessWurkerAuthor Commented:
Zyloch: So far, I've tried yours, and it almost works. The problem is. If I ignore the alert message, the extra returns I entered remain, and I'm able to submit a faulty form.
0
 
dakydCommented:
This should prevent the returns and semi-colons from showing up in the textarea:
<TEXTAREA name="Keywords" wrap="Virtual" rows="4" cols="24" onkeypress="return validateText(event);"></TEXTAREA>

Hope that helps.
0
 
GessWurkerAuthor Commented:
justinbillig:

A slight modification to your suggestion did the trick. Thanks!!
0
 
GessWurkerAuthor Commented:
Meant to post the modified version:
function CancelInvalidKeys( )
{
     // Assume True
     var blnReturnValue = true;
     if( event.keyCode == 59 ) // semicolan
     {
          blnReturnValue = false;
           alert("Please do not enter semi-colons in this field.")
     }
     else if( event.keyCode == 13 ) // enter
     {
          blnReturnValue = false;
           alert("Please do not enter extra returns in this field.")
     }
     return blnReturnValue;
}

0
 
ZylochCommented:
I would suggest you modify it to this:

function CancelInvalidKeys(e)
{

   var blnReturnValue = true;
   var the_key = (window.event) ? window.event.keyCode : e.which;

   if (the_key == 59) {
      blnReturnValue = false;
      alert("Please do not enter semi-colons in this field.");
   }
   else if (the_key == 13) {
      blnReturnValue = false;
      alert("Please do not enter extra returns in this field.");
   }
   return blnReturnValue;

}


Then call it like onkeypress = CancelInvalidKeys(event)

This will ensure it works in both IE and Mozilla/Netscape
0
 
GessWurkerAuthor Commented:
zyloch:

You're absolutely right! And I obviously forgot to check things out in Netscape this time. Dang! Perhaps I should request a re-allocation? Split points? What do you think?
0
 
ZylochCommented:
Nah, doesn't matter, I'm just making sure you don't make it IE-only (the most tragic thing in the world ;)
0
 
GessWurkerAuthor Commented:
Well...I appreciate your vigilance. I usually check Netscape compatibility because a few rebel souls where I work still insist on using it. I used to be one of those rebel souls but eventually gave it up. (shame!)

Anyway, thanks again.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.