By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,786 Members | 2,241 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,786 IT Pros & Developers. It's quick & easy.

How to allow CARRIAGE RETURN and BACKSPACE?

P: n/a
This JS limits the input characters into the form. How do I modify it
so that it also allows CARRIAGE RETURN and BACKSPACE (for making text
correction)?

Due to the template engine I am using, I cannot use IF/ELSE statement.

==============================
<form>

<textarea name="event_description" ONKEYPRESS="if (document.layers)
var c = event.which;
else if (document.all)
var c = event.keyCode;
else
var c = event.charCode;
var s = String.fromCharCode(c);
return /[0-9a-zA-Z\s,.?!@#$%&*()-]/.test(s);"></
textarea>
</form>

May 22 '07 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Blue wrote:
This JS limits the input characters into the form. How do I modify it
so that it also allows CARRIAGE RETURN and BACKSPACE (for making text
correction)?

Due to the template engine I am using, I cannot use IF/ELSE statement.

==============================
<form>

<textarea name="event_description" ONKEYPRESS="if (document.layers)
var c = event.which;
else if (document.all)
var c = event.keyCode;
else
var c = event.charCode;
var s = String.fromCharCode(c);
return /[0-9a-zA-Z\s,.?!@#$%&*()-]/.test(s);"></
textarea>
</form>
Hi,

At the moment you use a regular expression approach.
You can keep that, but first check for keyvalues CARRIAGE RETURN and
BACKSPACE, and return true if one of them.

So you'll end up with something like:

<textarea name="event_description" ONKEYPRESS="if (document.layers)
var c = event.which;
else if (document.all)
var c = event.keyCode;
else
var c = event.charCode;
var s = String.fromCharCode(c);
if (s == 13) {return true;}
if (s == 32) {return true;}
return /[0-9a-zA-Z\s,.?!@#$%&*()-]/.test(s);"></

The 13 is ENTER
The 32 is space, not BACKSPACE.
I don't remember the number of BACKSPACE. You'll have to find the number
yourself. (Or use alert(s) to test).
Regards,
Erwin Moller
May 23 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.