how can I create a field that can only insert the ascii symbol

S

SAN CAZIANO

how can I create a function like this that let me insert only symbol (in
ascii code the symbol is from 33 to 47 and from 58 to 64 and from 91 to 96
and from 123 to 127)

function CampoSoloNumerico(evt)
{
evt = (evt) ? evt : event;
var charCode = (evt.charCode) ? evt.charCode : ((evt.keyCode) ?
evt.keyCode : ((evt.which) ? evt.which : 0));
if (charCode > 31 && (charCode < 48 || charCode > 57))
{
alert("Puoi inserire solo numeri!");
return false;
}
return true;
}
 
M

Michael Winter

how can I create a function like this that let me insert only symbol (in
ascii code the symbol is from 33 to 47 and from 58 to 64 and from 91 to
96 and from 123 to 127)

In other words, no control characters, spaces, numbers, or letters? That
doesn't match what your code is attempting to do.

If you validated the control with more traditional methods, that would
become:

function funcName(elem) {
if(/^[^\x00-\x20 0-9a-z]*$/i.test(elem.value)) {
// Add alert here
return false;
}
return true;
}

or if you are only looking for numbers,

function funcName(elem) {
if(/^\d*$/.test(elem.value)) {
// Add alert here
return false;
}
return true;
}

changing the asterisk (*) to a plus (+) if you want to force at least one
character. You'd that function with:

<input ... onchange="funcName(this);">

though it must be said that the return values wouldn't have any effect as
the change event cannot be cancelled.

The problem with restricting characters as they are typed, which is what
you seem to be wanting, is that not only can it fail to catch certain
cases (such as a copied value), but it can also confuse users if they make
a typing error and try and delete it. As the value was blocked, they'll
end up deleting a wanted character. It's also frustrating to be told every
time that you do make a typo that you've done so. Better to be told just
the once.

If you do want to persist in restricting keystrokes the less user-friendly
way, you can find them in the Google archives for this group.

Mike
 
E

Evertjan.

Michael Winter wrote on 07 okt 2004 in comp.lang.javascript:
function funcName(elem) {
if(/^\d*$/.test(elem.value)) {
// Add alert here
return false;
}
return true;
}

This is the same as:

function funcName(elem) {
return !/^\d*$/.test(elem.value)
}

or with an alert:

function funcName(elem) {
val t = /^\d*$/.test(elem.value)
if(t)alert('blah')
return !t
}

Just playing.
 
M

Michael Winter

On Thu, 07 Oct 2004 14:02:30 GMT, Michael Winter

[snip]
function funcName(elem) {
if(/^[^\x00-\x20 0-9a-z]*$/i.test(elem.value)) {

if(!/^[^\x00-\x200-9a-z]*$/i.test(elem.value)) {

or

if(!/^[^\x00-\x1f 0-9a-z]*$/i.test(elem.value)) {

or

if(/^[\x00-\x200-9a-z]*$/i.test(elem.value)) {

or

if(/^[\x00-\x1f 0-9a-z]*$/i.test(elem.value)) {
// Add alert here
return false;
}
return true;
}
[snip]

function funcName(elem) {
if(/^\d*$/.test(elem.value)) {

if(!/^\d*$/.test(elem.value)) {
// Add alert here
return false;
}
return true;
}

[snip]

Sorry about that.

Mike
 
M

Michael Winter

On 07 Oct 2004 14:18:12 GMT, Evertjan. <[email protected]>
wrote:

[snip]
function funcName(elem) {
val t = /^\d*$/.test(elem.value)
if(t)alert('blah')
return !t
}

Don't you mean:

val t = /^\d*$/.test(elem.value);
if(!t)alert('blah');
return t;

It was probably because my code was wrong.
Just playing.

Nothing wrong with that. :)

Mike
 

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,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top