GessWurker
asked on
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).toUppe rCase() + 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(aS tar[i]).st yle.displa y = "none";
}
msg = "";
for(i=0;i<aFld.length;i++) {
if(fObj[aFld[i]].value=="" ){
document.getElementById(aS tar[i]).st yle.displa y = "";
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.cu rsor='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>
<HEAD>
<LINK href="/wcSuggest/wcSuggest
<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).toUppe
}
}
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(aS
}
msg = "";
for(i=0;i<aFld.length;i++)
if(fObj[aFld[i]].value==""
document.getElementById(aS
msg += titleCase(aFld[i])+" is a required field.\n";
alert(msg);
document.wcSuggestion.site
}
}
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
<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.cu
</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>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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["wcSuggesti on"].Keywo rds.focus( );
return false;
}
}
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["wcSuggesti
return false;
}
}
ASKER
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.
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);"></TE XTAREA>
Hope that helps.
<TEXTAREA name="Keywords" wrap="Virtual" rows="4" cols="24" onkeypress="return validateText(event);"></TE
Hope that helps.
ASKER
justinbillig:
A slight modification to your suggestion did the trick. Thanks!!
A slight modification to your suggestion did the trick. Thanks!!
ASKER
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;
}
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;
}
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
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
ASKER
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?
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?
Nah, doesn't matter, I'm just making sure you don't make it IE-only (the most tragic thing in the world ;)
ASKER
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.
Anyway, thanks again.
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(e
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