Solved

prevent keystrokes "Enter" and semi-colon in textarea

Posted on 2004-08-10
11
1,398 Views
Last Modified: 2012-06-21
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>
0
Comment
Question by:GessWurker
11 Comments
 
LVL 36

Expert Comment

by:Zyloch
ID: 11767478
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
 
LVL 15

Accepted Solution

by:
justinbillig earned 500 total points
ID: 11767490
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
 
LVL 36

Expert Comment

by:Zyloch
ID: 11767539
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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 

Author Comment

by:GessWurker
ID: 11767707
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
 
LVL 19

Expert Comment

by:dakyd
ID: 11767769
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
 

Author Comment

by:GessWurker
ID: 11767785
justinbillig:

A slight modification to your suggestion did the trick. Thanks!!
0
 

Author Comment

by:GessWurker
ID: 11767793
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
 
LVL 36

Expert Comment

by:Zyloch
ID: 11767869
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
 

Author Comment

by:GessWurker
ID: 11767973
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
 
LVL 36

Expert Comment

by:Zyloch
ID: 11767980
Nah, doesn't matter, I'm just making sure you don't make it IE-only (the most tragic thing in the world ;)
0
 

Author Comment

by:GessWurker
ID: 11768005
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

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

778 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question