Pagina 1 di 2 1 2 ultimoultimo
Visualizzazione dei risultati da 1 a 10 su 15
  1. #1
    Utente di HTML.it
    Registrato dal
    Jan 2003
    Messaggi
    1,093

    attivare solo tasti consentiti

    Ciao a tutti,

    come faccio in js a fare in modo che l' utente in un campo di testo, riesca a digitare solo i tasti conle lettere consentite.

    ad esempio
    ho un campo testo telefono, voglio che all' utente non sia permesso l' inserimento di lettere ma solo numero quindi se digita da tastiera una letteram questa non appaia, come se il tasto fosse disabilitato..
    non so se mi sono spiegato..

    spero...

    grazie mille
    ciao

  2. #2
    Utente di HTML.it L'avatar di morphy79
    Registrato dal
    Jun 2004
    Messaggi
    1,568
    Da mettere nei campi di testo :

    Spero ti bastino !!! Ciao !!!!



    ' ACCETTA SOLO NUMERI INTERI POSITIVI (da 0 a 9)
    '
    onKeypress="if (event.keyCode < 48 || event.keyCode > 57) event.returnValue = false;"


    ' ACCETTA SOLO NUMERI DECIMALI POSITIVI ( decimali separati dalla virgola)
    '
    onKeypress="if ((event.keyCode < 48 || event.keyCode > 57) && event.keyCode != 44) event.returnValue = false;"


    ' ACCETTA SOLO INSERIMENTI DI ORE ( da 0 a 9 )
    '
    onKeypress="if (event.keyCode < 48 || event.keyCode > 57) event.returnValue = false;"


    ' ACCETTA SOLO INSERIMENTI DI DATE ( da 0 a 9 e "/" )
    '
    onKeypress="if (event.keyCode < 47 || event.keyCode > 57) event.returnValue = false;"


    ' ACCETTA SOLO INSERIMENTI DI NUMERI TELEFONICI ( da 0 a 9, "/" , "-" e "." )
    '
    onKeypress="if (event.keyCode < 45 || event.keyCode > 57) event.returnValue = false;"


    ' BLOCCA APICI SINGOLI
    '
    onKeypress="if (event.keyCode==39) event.returnValue = false;"


    ' BLOCCA DOPPIE VIRGOLETTE
    '
    onKeypress="if (event.keyCode==34) event.returnValue = false;"


    ' BLOCCA ALCUNI CARATTERI ( doppie virgolette e apici singoli )
    '
    onKeypress="if (event.keyCode==34 || event.keyCode==39) event.returnValue = false;


    ' BLOCCA CARATTERI SPECIALI ( tutti i caratteri sopra numeri)
    '
    onKeypress="if ((event.keyCode > 32 && event.keyCode < 48) || (event.keyCode > 57 && event.keyCode < 65) || (event.keyCode > 90 && event.keyCode < 97)) event.returnValue = false;"


    ' BLOCCA TUTTI CARATTERI SPECIALI ( la maggior parte, praticamente accetta solo numeri e lettere )
    '
    onKeypress="if ((event.keyCode > 32 && event.keyCode < 48 && event.keyCode != 39 && event.keyCode != 47) || (event.keyCode > 57 && event.keyCode < 65) || (event.keyCode > 90 && event.keyCode < 97) || (event.keyCode==124)) event.returnValue = false;"


    ' ACCETTA SOLO LETTERE
    '
    onKeypress=" & CHR(34) & "if ((event.keyCode > 32 && event.keyCode < 48 && event.keyCode != 39 && event.keyCode != 47) || (event.keyCode > 57 && event.keyCode < 65) || (event.keyCode >= 48 && event.keyCode <= 57) || (event.keyCode > 90 && event.keyCode < 97) || (event.keyCode==124)) event.returnValue = false;"

  3. #3
    Utente di HTML.it
    Registrato dal
    Jan 2003
    Messaggi
    1,093
    ma sei un maestro!..grazie mille

    bye

  4. #4
    Utente di HTML.it
    Registrato dal
    Jan 2003
    Messaggi
    1,093
    e se volessi mettere tutte le soluzioni all' interno di una funzione e richiamarla al caricamento della pagina, passandogli magari solo il nome del campo a cui applicare le limitazioni come cavolo faccio?...ti spiego i form mi vengono generati da un softwre e io mano nel codice non posso mettercela...

    ciao
    speremmu...

  5. #5
    Utente di HTML.it L'avatar di morphy79
    Registrato dal
    Jun 2004
    Messaggi
    1,568
    già che ci siamo... fatti questa pagina HTML...
    per ogni tasto che premi ti dice il corrispettivo KEYPRESS !!!

    ps.. Non l'ho fatta io...



    code:--------------------------------------------------------------------------------
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <HTML><HEAD><TITLE>DHTML Tutorial</TITLE>
    <META http-equiv=Content-Type content="text/html; charset=windows-1252"><LINK
    href="DHTML Tutorial_file/stylesheet.css" type=text/css rel=stylesheet>
    <META content="Microsoft FrontPage 5.0" name=GENERATOR></HEAD>
    <BODY onkeypress=GetChar() onkeydown=GetCode()>
    <DIV class=divhead>Capturing Keystrokes</DIV>


    It can be useful to determine which keys are being pressed on the keyboard
    and to take action on them. If data entry for a textbox, for example, should be
    restricted to numbers, then scripts can monitor the key presses and permit only
    number keys to enter digits. In positioning elements on a page, arrow keys can
    be used to incrementally move the object across the page.</P>


    <SPAN class=head2>The event Object</SPAN></P>


    The event object contains information about user or system events
    occuring within the browser. Among the types of events that are trapped are
    keystroke and mouse events occuring on the screen, in the browser window, in the
    document, or associated with any HTML element on the page. Any action
    surrounding the page is always represented by a property setting in the event
    object.</P>


    All keystroke events generate a <SPAN class=code>keyCode</SPAN>
    property setting. This property is an integer value representing the particular
    key that is pressed, and it is accessible by a script through the <SPAN
    class=code>event.keyCode</SPAN> property.</P>


    <SPAN class=head2>Keyboard Event Handlers</SPAN></P>


    A key press is captured by two event handlers which return slightly different
    <SPAN class=code>keyCode</SPAN> values.</P>
    <TABLE cellPadding=3 border=1>
    <TBODY>
    <TR>
    <TH>Event Handler</TH>
    <TH>Returned Value</TH></TR>
    <TR>
    <TD class=code>onKeyDown</TD>
    <TD>Returns the integer <SPAN class=code>keyCode</SPAN> representing the
    value associated with the physical key</TD></TR>
    <TR>
    <TD class=code>onKeyPress</TD>
    <TD>Returns the integer <SPAN class=code>keyCode</SPAN> representing the
    Unicode value of the character associated with the key</TD></TR></TBODY></TABLE>


    Every key has a unique <SPAN class=code>keyCode</SPAN> value identifying the
    particular key that is pressed. This value is returned by the <SPAN
    class=code>onKeyDown</SPAN> handler. Keys that produce alphnumeric
    characters have <SPAN class=code>keyCode</SPAN> values identifying the
    associated characters. These Unicode values are returned by the <SPAN
    class=code>onKeyPress</SPAN> handler.</P>


    For character keys, a <SPAN class=code>keypress</SPAN> event can produce the
    same <SPAN class=code>keyCode</SPAN> for two different keys, even though a <SPAN
    class=code>keydown</SPAN> event produces different codes. For example, the "1"
    key along the top of the keyboard produces a <SPAN class=code>keyCode=49</SPAN>
    when <SPAN class=code>onKeyDown </SPAN>captures the keystroke. The "1" key on
    the numeric keypad, however, produces a <SPAN class=code>keyCode=97</SPAN> since
    this is a physically different key. Both keys, though, produce a <SPAN
    class=code>keyCode=49</SPAN> when captured with <SPAN
    class=code>onKeyPress</SPAN> since the "1" character is produced by both
    keys.</P>
    <SCRIPT>
    function GetCode()
    {
    document.all.KeyCode.innerText = event.keyCode
    document.all.KeyChar.innerHTML = " "
    document.all.Character.innerHTML = " "
    }
    function GetChar()
    {
    document.all.KeyChar.innerText = event.keyCode
    var Character = event.keyCode
    document.all.Character.innerText = String.fromCharCode(Character)
    }
    </SCRIPT>

    <TABLE
    style="FLOAT: right; MARGIN-LEFT: 20px; POSITION: relative; BORDER-COLLAPSE: collapse"
    cellPadding=3 border=1>
    <TBODY>
    <TR>
    <TH colSpan=2>keyCode</TH>
    <TH vAlign=bottom rowSpan=2>Character</TH></TR>
    <TR>
    <TH>onKeyDown</TH>
    <TH>onKeyPress</TH></TR>
    <TR align=middle>
    <TD><SPAN id=KeyCode> </SPAN></TD>
    <TD><SPAN id=KeyChar> </SPAN></TD>
    <TD><SPAN id=Character> </SPAN></TD></TR></TBODY></TABLE>


    The relationships between the <SPAN class=code>keyCodes</SPAN> produced by
    the two events are given in the accompanying table. Press any key to view its
    codes along with the character that is represented by the key.</P>


    <SPAN class=head2>Capturing keyCodes with onKeyDown</SPAN></P>


    If you have a textbox for entry of, say, numeric characters, you can check
    the user's keystrokes to make sure that only numbers are entered into the box.
    One solution involves associating (binding) the <SPAN
    class=code>onKeyDown</SPAN> event hander with the textbox to capture the <SPAN
    class=code>keyCode</SPAN> for the physical key that is pressed. If the <SPAN
    class=code>keyCode</SPAN> is not one of the number keys, then entry is not
    permitted.</P>


    The following textbox works in this fashion. Enter numbers or other
    characters into the box. Only numbers or a hyphen are permitted.</P>Zip Code:
    <INPUT onkeydown="return CheckKeyCode()" style="WIDTH: 100px" size="20">
    <SCRIPT>
    function CheckKeyCode()
    {
    if( (event.keyCode == 189 || event.keyCode == 109) ||
    (event.keyCode >= 48 && event.keyCode <= 57) ||
    (event.keyCode >= 96 && event.keyCode <= 105) ) {
    return true }
    else {
    return false
    }
    }
    </SCRIPT>




    <DIV class=divcode><SPAN class=script>&lt;SCRIPT&gt;
    function
    CheckKeyCode()
    {
    if( (event.keyCode == 189 || event.keyCode ==
    109) ||
    (event.keyCode &gt;= 48 &&
    event.keyCode &lt;= 57) ||

    (event.keyCode &gt;= 96 &&
    event.keyCode &lt;= 105) ) {
    return true
    }
    else {
    return
    false
    }
    }
    &lt;/SCRIPT&gt;
    </SPAN>
    Zip Code: &lt;input
    type="text" style="width:100px"
    onKeyDown=<SPAN
    class=script>"return CheckKeyCode()"</SPAN>/&gt;
    </DIV>


    The textbox includes an <SPAN class=code>onKeyDown</SPAN> event hander to
    respond to a keystroke and the capture the <SPAN class=code>keyCode</SPAN> for
    the physical key that is pressed. A function call is made to <SPAN
    class=code>CheckKeyCode()</SPAN>, which returns <SPAN class=code>true</SPAN>
    (enter the keystroke into the textbox) or <SPAN class=code>false</SPAN> (do not
    enter the keystroke into the textbox) depending on whether or not a number key
    or a hyphen is typed.


    As you type in the above textbox these keyboard events are being recorded in
    the event object as the <SPAN class=code>event.keyCode</SPAN> property.
    Therefore, the script tests this property to discover which key was pressed. The
    number keys along the top of the keyboard have <SPAN class=code>keyCode</SPAN>
    values of <SPAN class=code>48</SPAN> - <SPAN class=code>57</SPAN> for the "0"
    through "9" keys. The keys on the number pad have <SPAN
    class=code>keyCode</SPAN> values of <SPAN class=code>96</SPAN> - <SPAN
    class=code>105</SPAN> for the ten number keys. The hyphen key at the top of the
    keyboard has a <SPAN class=code>keyCode</SPAN> value of <SPAN
    class=code>189</SPAN>; the minus (<SPAN class=code>-</SPAN> ) key on the number
    pad (used also as a hyphen) has a <SPAN class=code>keyCode</SPAN> of <SPAN
    class=code>109</SPAN>.</P>


    These are the valid <SPAN class=code>keyCode</SPAN> values checked in the
    script. If any of these keys are pressed, the function returns <SPAN
    class=code>true</SPAN> and the keystroke produces the character in the textbox.
    Otherwise, the function returns <SPAN class=code>false</SPAN> and the character
    is not produced.</P>


    <SPAN class=head2>Capturing keyCodes with onKeyPress</SPAN></P>


    The <SPAN class=code>onKeyPress</SPAN> event handler produces <SPAN
    class=code>keyCodes</SPAN> representing the character that is typed. For
    non-character keys no <SPAN class=code>keyCode</SPAN> is produced. These
    character <SPAN class=code>keyCodes</SPAN> represent the Unicode values of the
    characters and present a second way to check for valid entry into a textbox.</P>


    The following textbox, for example, only permits entry of upper-case
    alphabetic characters.</P>State Code:
    <INPUT onkeypress="return CheckAlpha()"
    style="WIDTH: 25px" maxLength=2 size="20">
    <SCRIPT>
    function CheckAlpha()
    {
    if (event.keyCode >= 65 && event.keyCode <= 90) {
    return true }
    else {
    return false
    }

    }
    </SCRIPT>




    <DIV class=divcode><SPAN class=script>&lt;SCRIPT&gt; function
    CheckAlpha()
    {
    if (event.keyCode &gt;= 65 &&
    event.keyCode &lt;= 90) {
    return true
    }
    else {
    return
    false
    }
    }
    &lt;/SCRIPT&gt;
    </SPAN>
    State Code:
    &lt;input type="text" style="width:25px"
    maxlength="2"
    onKeyPress=<SPAN class=script>"return
    CheckAlpha()"</SPAN>/&gt;
    </DIV>


    </P>


    Since an <SPAN class=code>onKeyPress</SPAN> event handler is used with the
    textbox, the <SPAN class=code>keyCode</SPAN> values are the character codes
    associated with the keys. The characters <SPAN class=code>"a" - "z"</SPAN> have
    <SPAN class=code>keyCodes</SPAN> of <SPAN class=code>97 - 122</SPAN>; the
    upper-case characters <SPAN class=code>"A" - "Z"</SPAN> have <SPAN
    class=code>keyCodes</SPAN> of <SPAN class=code>65 - 90</SPAN>. These latter
    codes are checked by the script to return <SPAN class=code>true</SPAN> or <SPAN
    class=code>false</SPAN>.</P>


    <SPAN class=head2>Checking Characters</SPAN></P>


    A third way of testing keystrokes is to discover the actual character that is
    typed. This method uses the <SPAN class=code>onKeyPress</SPAN> event handler to
    get the Unicode values, which are then converted to ASCII characters. The
    following textbox perform the same function as the previous textbox except that
    the script checks for the characters "A" through "Z" rather than the codes 65
    through 90.</P>State Code:
    <INPUT onkeypress="return CheckCharacter()"
    style="WIDTH: 25px" maxLength=2 size="20">
    <SCRIPT>
    function CheckCharacter()
    {
    if (String.fromCharCode(event.keyCode) >= "A" && String.fromCharCode(event.keyCode) <= "Z" ) {
    return true }
    else {
    return false
    }
    }
    </SCRIPT>




    <DIV class=divcode><SPAN class=script>&lt;SCRIPT&gt; function
    CheckCharacter()
    {
    if (String.fromCharCode(event.keyCode)
    &gt;= "A" &&

    String.fromCharCode(event.keyCode) &lt;=
    "Z" ) {
    return true }
    else
    {
    return
    false
    }
    }
    &lt;/SCRIPT&gt;
    </SPAN>
    State Code:
    &lt;input type="text" style="width:25px"
    maxlength="2"
    onKeyPress=<SPAN class=script>"return
    CheckCharacter()"</SPAN>/&gt;
    </DIV>


    </P>


    Unicode values are converted to ASCII characters with the <SPAN
    class=code>String.fromCharCode(Unicode )</SPAN> method. The
    supplied Unicode value is return as the keyboard character. In the above
    script, the <SPAN class=code>event.keyCode</SPAN> is returned as its associated
    character and checked whether it falls in the range <SPAN
    class=code>"A"-"Z"</SPAN>.</P>


    ELENCO CARATTERI SPECIALI ( chr ) :</P>


    <font class="contentFont"><font class="codeFont"><font color="#ff0000">
    <font class="regularFont">Chr(33) = !

    Chr(34) = "

    Chr(35) = #

    Chr(36) = $

    Chr(37) = %

    Chr(38) = &

    Chr(39) = '

    Chr(40) = (

    Chr(41) = )

    Chr(42) = *

    Chr(43) = +

    Chr(44) = ,

    Chr(47) = /

    Chr(58) = :

    Chr(59) = ;

    Chr(60) = &lt;

    Chr(61) = =

    Chr(62) = &gt;

    Chr(63) = ?

    Chr(91) = [

    Chr(92) = \

    Chr(93) = ]

    Chr(94) = ^

    Chr(96) = `

    Chr(123) = {</font></font></font></font></P>
    </BODY></HTML>

  6. #6
    Utente di HTML.it L'avatar di morphy79
    Registrato dal
    Jun 2004
    Messaggi
    1,568
    io faccio così via asp :

    ho questo codice in un include...
    poi su ciascun campo scrivo il CLASS SOLTANTO

    es.

    <%=classDATA%>

    codice:
    <%
    ' ACCETTA SOLO NUMERI INTERI POSITIVI (da 0 a 9)
    '
    	classINT = "onKeypress=" & CHR(34) & "if (event.keyCode < 48 || event.keyCode > 57) event.returnValue = false;" & CHR(34)
    
    
    ' ACCETTA SOLO NUMERI DECIMALI POSITIVI ( decimali separati dalla virgola)
    '
    	classDEC = "onKeypress=" & CHR(34) & "if ((event.keyCode < 48 || event.keyCode > 57) && event.keyCode != 44) event.returnValue = false;" & CHR(34)
    
    
    ' ACCETTA SOLO INSERIMENTI DI ORE ( da 0 a 9 )
    '
    	classORA = "onKeypress=" & CHR(34) & "if (event.keyCode < 48 || event.keyCode > 57) event.returnValue = false;" & CHR(34)
    
    
    ' ACCETTA SOLO INSERIMENTI DI DATE ( da 0 a 9 e "/" )
    '
    	classDATA = "onKeypress=" & CHR(34) & "if (event.keyCode < 47 || event.keyCode > 57) event.returnValue = false;" & CHR(34)
    
    
    ' ACCETTA SOLO INSERIMENTI DI NUMERI TELEFONICI ( da 0 a 9, "/" , "-" e "." )
    '
    	classTEL = "onKeypress=" & CHR(34) & "if (event.keyCode < 45 || event.keyCode > 57) event.returnValue = false;" & CHR(34)
    
    
    ' BLOCCA APICI SINGOLI
    '
    	classAPI = "onKeypress=" & CHR(34) & "if (event.keyCode==39) event.returnValue = false;" & CHR(34)
    
    
    ' BLOCCA DOPPIE VIRGOLETTE
    '
    	classVIRG = "onKeypress=" & CHR(34) & "if (event.keyCode==34) event.returnValue = false;" & CHR(34)
    
    
    ' BLOCCA ALCUNI CARATTERI ( doppie virgolette e apici singoli )
    '
    	classSTR1 = "onKeypress=" & CHR(34) & "if (event.keyCode==34 || event.keyCode==39) event.returnValue = false;" & CHR(34)
    
    
    ' BLOCCA CARATTERI SPECIALI ( tutti i caratteri sopra numeri)
    '
    	classSTR2 = "onKeypress=" & CHR(34) & "if ((event.keyCode > 32 && event.keyCode < 48) || (event.keyCode > 57 && event.keyCode < 65) || (event.keyCode > 90 && event.keyCode < 97)) event.returnValue = false;" & CHR(34)
    
    
    ' BLOCCA TUTTI CARATTERI SPECIALI ( la maggior parte, praticamente accetta solo numeri e lettere )
    '
    	classSTR3 = "onKeypress=" & CHR(34) & "if ((event.keyCode > 32 && event.keyCode < 48 && event.keyCode != 39 && event.keyCode != 47) || (event.keyCode > 57 && event.keyCode < 65) || (event.keyCode > 90 && event.keyCode < 97) || (event.keyCode==124)) event.returnValue = false;" & CHR(34) 
    
    
    ' ACCETTA SOLO LETTERE
    '
    	classSTR4 = "onKeypress=" & CHR(34) & "if ((event.keyCode > 32 && event.keyCode < 48 && event.keyCode != 39 && event.keyCode != 47) || (event.keyCode > 57 && event.keyCode < 65) || (event.keyCode >= 48 && event.keyCode <= 57) || (event.keyCode > 90 && event.keyCode < 97) || (event.keyCode==124)) event.returnValue = false;" & CHR(34) 
    
    
    %>

  7. #7
    Utente di HTML.it
    Registrato dal
    Jan 2003
    Messaggi
    1,093
    in teroria avrei bisogno di qualcosa del tipo
    funzione a cui passo nome campo
    e a quel campo lui applica le limitazioni, solo a quel campo, e il problema è che devo servirmi solo del nome,non posso aggiungere nulla ai codici dei campi capisci..li il casino...

    cazz******

    grzie

  8. #8
    Utente di HTML.it L'avatar di morphy79
    Registrato dal
    Jun 2004
    Messaggi
    1,568
    si può fare...

    facciamo un'array bidimensionale con tutti i nomi dei tuoi campi e la tipologia che devono avere
    la funzione scorre l'array e per ciascun campo fa via javascript insert HTML della rispettiva tipologia sul campo...
    VVoVe:

  9. #9
    Utente di HTML.it
    Registrato dal
    Jan 2003
    Messaggi
    1,093
    ma qualcosa di piu dinamico...nonlegato al nome di tutti i campi, e utilizzabile in tutte ma tutte le pagine possibili immaginabili, con un campo e con 30 campi, con un campo connome pluto e uno con nome prodotti, capisci, tipo una cosa del genere:

    <html>
    <head>
    <script>
    function cambia()
    {
    document.form1.pippo.onKeypress="if (event.keyCode < 48 ||

    event.keyCode > 57) event.returnValue = false;"

    }
    </script>
    </head>
    <body onload="cambia()">
    <input type="text" name="pippo">
    </body>
    </html>


    adesso è senza argomenti perchè facevo prova, ma passandogli nome campo e nome form, diventa generalissima...non credi?

  10. #10
    Utente di HTML.it
    Registrato dal
    Jan 2003
    Messaggi
    1,093
    ah dimenticavo..NON FUNZIONA IL MIO ESEMPIO

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.