Home     |     .Net Programming    |     cSharp Home    |     Sql Server Home    |     Javascript / Client Side Development     |     Ajax Programming

Ruby on Rails Development     |     Perl Programming     |     C Programming Language     |     C++ Programming     |     IT Jobs

Python Programming Language     |     Laptop Suggestions?    |     TCL Scripting     |     Fortran Programming     |     Scheme Programming Language


 
 
Cervo Technologies
The Right Source to Outsource

MS Dynamics CRM 3.0

Javascript / Client Side Development

How to allow CARRIAGE RETURN and BACKSPACE?


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

Add to del.icio.us | Digg this | Stumble it | Powered by Megasolutions Inc