How to allow CARRIAGE RETURN and BACKSPACE?

B

Blue

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>
 
E

Erwin Moller

Blue said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top