JS auf alle Textfelder erweitern

Diese Seite verwendet Cookies. Durch die Nutzung unserer Seite erklären Sie sich damit einverstanden, dass wir Cookies setzen. Weitere Informationen

  • JS auf alle Textfelder erweitern

    Hallo,

    habe folgendes JS im Netzt gefunden (lässte die Eingabe von nur bestimmten Zeichen im Textefeld zu) das für meine Zwecke auch Ideal ist. Da ich aber mehrere Textfelder habe und das Script leider nur beim ersten Feld funktioniert brauche ich eine Hilfestellung zum abändern des JS.

    Gruß
    mactoni

    HTML-Quellcode

    1. <html>
    2. <head>
    3. <script type="text/javascript">
    4. MS_restrict_field = function(formname, id_or_name, chars) {
    5. var obj = (document.getElementById && document.getElementById(id_or_name) != null)
    6. ? document.getElementById(id_or_name) : ((document[formname][id_or_name] != null)
    7. ? document[formname][id_or_name] : '');
    8. if(obj.type == "text" || obj.type == "textarea") {
    9. obj.timer = "";
    10. obj.chars = chars;
    11. obj.onkeypress = obj.onkeydown = function() {
    12. var self = this;
    13. controll = function() {
    14. for(var t='',x=0; x<self.value.length; ++x) {
    15. if(self.chars.indexOf(self.value.charAt(x))>-1 ) {
    16. t += self.value.charAt(x);
    17. } else
    18. alert('Folgende Eingaben sind möglich:\n0 = Falsch\n1 = Richtig\n2 = keine Antwort')
    19. }
    20. self.value = t;
    21. };
    22. this.timer = setTimeout(controll,1);
    23. };
    24. obj.onkeyup = function() {
    25. clearTimeout(this.timer);
    26. };
    27. }
    28. };
    29. </script>
    30. </head>
    31. <body onLoad="MS_restrict_field('restrictform','chkfield','.012')">
    32. <form name="restrictform" >
    33. <input type="text" name="field" id="chkfield" />
    34. <input type="text" name="field" id="chkfield" />
    35. <input type="text" name="field" id="chkfield" />
    36. <input type="text" name="field" id="chkfield" />
    37. <input type="text" name="field" id="chkfield" />
    38. <input type="text" name="field" id="chkfield" />
    39. <input type="text" name="field" id="chkfield" />
    40. <input type="text" name="field" id="chkfield" />
    41. </form>
    42. </body>
    43. </html>
    Alles anzeigen
  • das ist mein letzter Stand:
    hier geht seltsamerweise immer nur jedes 2. Feld. Bin ratlos

    Quellcode

    1. <body onLoad="MS_restrict_field('restrictform','chkfield','.012')">
    2. <form name="restrictform" onkeyup="MS_restrict_field('restrictform','chkfield1','.012')">
    3. <input type="text" name="field" id="chkfield" />
    4. <input type="text" name="field" id="chkfield1" onkeyup="MS_restrict_field(this,'chkfield2','.012')" />
    5. <input type="text" name="field" id="chkfield2" onkeyup="MS_restrict_field(this,'chkfield3','.012')" />
    6. <input type="text" name="field" id="chkfield3" onkeyup="MS_restrict_field(this,'chkfield4','.012')" />
    7. <input type="text" name="field" id="chkfield4" onkeyup="MS_restrict_field(this,'chkfield5','.012')" />
    8. <input type="text" name="field" id="chkfield5" onkeyup="MS_restrict_field(this,'chkfield6','.012')" />
    9. <input type="text" name="field" id="chkfield6" onkeyup="MS_restrict_field(this,'chkfield7','.012')" />
    10. <input type="text" name="field" id="chkfield7" onkeyup="MS_restrict_field(this,'chkfield8','.012')" />
    11. <input type="text" name="field" id="chkfield7" onkeyup="if(this.form.chkfield.value.length == 2) {this.form.chkfield7.focus()}" />
    12. </form>
    13. </body>
    Alles anzeigen
  • Ok, nachdem ich den ganzen Nachmittag jetzt herumgetestet habe, hab ich nun endlich die Lösung gefunden, ob's elegant ist ist mir jetzt ganz egal, es funktioniert auf jeden Fall zuverlässig:

    HTML-Quellcode

    1. <html>
    2. <head>
    3. <script type="text/javascript">
    4. MS_restrict_field = function(formname, id_or_name, chars) {
    5. var obj = (document.getElementById && document.getElementById(id_or_name) != null)
    6. ? document.getElementById(id_or_name) : ((document[formname][id_or_name] != null)
    7. ? document[formname][id_or_name] : '');
    8. if(obj.type == "text" || obj.type == "textarea") {
    9. obj.timer = "";
    10. obj.chars = chars;
    11. obj.onkeypress = obj.onkeydown = function() {
    12. var self = this;
    13. controll = function() {
    14. for(var t='',x=0; x<self.value.length; ++x) {
    15. if(self.chars.indexOf(self.value.charAt(x))>-1 ) {
    16. t += self.value.charAt(x);
    17. } else
    18. alert('Folgende Eingaben sind möglich:\n0 = Falsch\n1 = Richtig\n2 = keine Antwort')
    19. }
    20. self.value = t;
    21. };
    22. this.timer = setTimeout(controll,1);
    23. };
    24. obj.onkeyup = function() {
    25. clearTimeout(this.timer);
    26. };
    27. }
    28. };
    29. function jumpToTextField(activeTextfield, nextTextfieldId) {
    30. if (activeTextfield.value.length > 0)
    31. document.getElementById(nextTextfieldId).focus();
    32. }
    33. </script>
    34. </head>
    35. <body onLoad="MS_restrict_field('restrictform','chkfield0','.012')">
    36. <form name="restrictform" >
    37. <input type="text" name="field" id="chkfield0" onkeyup="MS_restrict_field(this,'chkfield0','.012')"; jumpToTextField(this, 'chkfield1'); maxlength="1"/>
    38. <input type="text" name="field" id="chkfield1" onkeyup="MS_restrict_field(this,'chkfield1','.012')"; jumpToTextField(this, 'chkfield2'); maxlength="1"/>
    39. <input type="text" name="field" id="chkfield2" onkeyup="MS_restrict_field(this,'chkfield2','.012')"; jumpToTextField(this, 'chkfield3'); maxlength="1"/>
    40. <input type="text" name="field" id="chkfield3" onkeyup="MS_restrict_field(this,'chkfield3','.012')"; jumpToTextField(this, 'chkfield4'); maxlength="1"/>
    41. <input type="text" name="field" id="chkfield4" onkeyup="MS_restrict_field(this,'chkfield4','.012')"; jumpToTextField(this, 'chkfield5'); maxlength="1"/>
    42. <input type="text" name="field" id="chkfield5" onkeyup="MS_restrict_field(this,'chkfield5','.012')"; jumpToTextField(this, 'chkfield6'); maxlength="1"/>
    43. <input type="text" name="field" id="chkfield6" onkeyup="MS_restrict_field(this,'chkfield6','.012')"; jumpToTextField(this, 'chkfield7'); maxlength="1"/>
    44. <input type="text" name="field" id="chkfield7" onkeyup="MS_restrict_field(this,'chkfield7','.012')"; jumpToTextField(this, 'chkfield8'); maxlength="1"/>
    45. <input type="text" name="field" id="chkfield8" onkeyup="MS_restrict_field(this,'chkfield8','.012')"; jumpToTextField(this, 'chkfield9'); maxlength="1"/>
    46. </form>
    47. </body>
    48. </html>
    Alles anzeigen