Thursday, July 29th 2010, 1:32pm UTC+2

You are not logged in.

  • Login
  • Register

womstar

Professional

Posts: 619

Location: latente matrix

1

Wednesday, April 27th 2005, 7:10pm

Nur bestimmte Zeichen in einem Eingabefeld zulassen

Mit dieser Funktion ist es möglich, dass ein Formularfeld nur bestimmte Zeichen akzeptiert.

Anregungen, Tipps und Verbesserungsvorschläge sind willkommen.


Bei diesem Beispiel werden nur Zahlen und Punkt akzeptiert:

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<html>
<head>
<script type="text/javascript">
MS_restrict_field = function(formname, id_or_name, chars) {

	var obj = (document.getElementById && document.getElementById(id_or_name) != null)
			  ? document.getElementById(id_or_name) : ((document[formname][id_or_name] != null)
			  ? document[formname][id_or_name] : '');

	if(obj.type == "text" || obj.type == "textarea") {

		obj.timer = "";
		obj.chars = chars;
		obj.onkeypress = obj.onkeydown = function() {
			var self = this;
			controll = function() {
				for(var t='',x=0; x<self.value.length; ++x) {
					if(self.chars.indexOf(self.value.charAt(x))>-1) {
						t += self.value.charAt(x);
					}
				}
				self.value = t;
			};
			this.timer = setTimeout(controll,1);
		};
		obj.onkeyup = function() {
			clearTimeout(this.timer);
		};
	}
};

</script>
</head>
<body onload="MS_restrict_field('restrictform','field','.0123456789')">
<form name="restrictform">
	<input type="text" name="field" id="field" />
</form>
</body>
</html>


Online Beispiel:
Nur bestimmte Zeichen in einem Eingabefeld zulassen


Viele Grüße,
Robert
  • Go to the top of the page

kelor

Super Moderator

Posts: 3,408

Location: formel1-stadt hockenheim

2

Thursday, April 28th 2005, 7:51am

sag mal...gibt es in js nicht auch die funktionalität 'restrict()'...?

muss ich gleich mal schauen gehen...


greetz

kelor
[ActionScript-Trainer] [Referent ActionScript] [Buchautor]
[Online/Offline Flash_Applications & Developer]
  • Go to the top of the page

womstar

Professional

Posts: 619

Location: latente matrix

3

Thursday, April 28th 2005, 2:25pm

Hallo Kelor,

nein, leider nicht.
Ich wäre ja dafür, dass W3C den Input-Type "Number" einführt oder ein Attribut wo man angeben kann, welche Zeichen erlaubt sind.
Das würde einiges erleichtern.

Viele Grüße,
Robert
  • Go to the top of the page

kelor

Super Moderator

Posts: 3,408

Location: formel1-stadt hockenheim

4

Thursday, April 28th 2005, 5:46pm

..interresant...denn in AS( dass ja JS sejr ähnelt) gibt es ja die methode restrict für textfelder...

greetz

kelor
[ActionScript-Trainer] [Referent ActionScript] [Buchautor]
[Online/Offline Flash_Applications & Developer]
  • Go to the top of the page

sonar

Beginner

Posts: 55

Location: München

5

Thursday, April 28th 2005, 7:11pm

Soweit ich weiß, kann man wirklich nur die max. Zeichenlänge für das Textfeld vorgeben... alles andere müsste man sich selber bauen... hier noch ne Alternative, hier würden nur a-z und 0-9 zugelassen:

Source code

1
2
3
4
5
6
7
8
9
10
// JS
function restrict (pTextField) {
  var allowedChars = /[0-9a-z]/;
    if (!allowedChars.test (pTextField.value)){
      pTextField.value = pTextField.value.replace (/([^0-9a-z])/g, "");
  }
}

<!-- HTML -->
<input type="text" onkeydown="restrict (this);" onkeyup="restrict (this);" onblur="restrict (this);" onclick="restrict (this);" />


Hi kelor :)

This post has been edited 1 times, last edit by "sonar" (Apr 28th 2005, 7:12pm)

  • Go to the top of the page

undefined

Super Moderator

Posts: 4,248

Location: Germany

6

Thursday, April 28th 2005, 7:27pm

@Kelor
Html und Javascript sind zwei Paar Schuhe ;)

ps: Siehe im Script abschnitt Mail check ;)
Formular Check[2]
Undefined Behavior (undefiniertes Verhalten) bedeutet meistens etwas ungültiges.
PHP Katepart - Speichenrechner - .htpasswd - RPM XDG Tool - Kcmnvview - QTidy
  • Go to the top of the page

kelor

Super Moderator

Posts: 3,408

Location: formel1-stadt hockenheim

7

Thursday, April 28th 2005, 7:57pm

ehmm..jürgen...
womstar hat doch das ganze nnerhalb eines js laufen und nicht als reines html...8o



greetz

kelor
[ActionScript-Trainer] [Referent ActionScript] [Buchautor]
[Online/Offline Flash_Applications & Developer]
  • Go to the top of the page