Solved

prevent keystrokes "Enter" and semi-colon in textarea

Posted on 2004-08-10
11
1,389 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
 

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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…

706 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now