W3C home > Mailing lists > Public > whatwg@whatwg.org > February 2006

[whatwg] Web Forms 2.0 proposal

From: Sean Hogan <shogun70@westnet.com.au>
Date: Sat, 11 Feb 2006 09:43:41 +1100
Message-ID: <43ED171D.50406@westnet.com.au>
Ian Hickson wrote:

>On Sat, 11 Feb 2006, Sean Hogan wrote:
>  
>
>>I would request a input filter as a regex range eg a-zA-Z0-9 i.e.  if 
>>the keyCode is NOT within the range then preventDefault(). I imagine 
>>that number, date, datetime, etc will be special implementations that 
>>filter keyboard input. Why not extend the facility to text, etc?
>>    
>>
>
>This is already possible using the pattern="" attribute:
>
>   http://whatwg.org/specs/web-forms/current-work/#pattern
>
>Thanks,
>  
>
I expressed myself poorly. 

If I'm reading the spec correctly then "pattern" is used to 
prevent/allow form submission, and to highlight an invalid entry. 

What I am suggesting is a filter that can be matched against for each 
keypress event. 
If keyCode matches then the character is appended to the input as normal. 
If it doesn't then the character is dropped on the floor. 

Here is some code which may be correct for current W3C browsers. 
The intention here is to only accept alphanumeric characters. 

<input type="text" filter="a-zA-Z0-9" onkeypress="inputfilter" />
<script>
    inputfilter(e) {
        var n = e.keyCode || e.which; // keyCode is 0 for mozilla.  use 
which.
        if (n==8 || n==9 || n==16 || n==17 || n==37 || n==39 || n==46) 
return true; // don't filter control characters
        var str = String.fromCharCode(n);
        var filter = this.getAttribute("filter");
        var r = new RegExp("["+filter+"]"); 
        if (r.test(str)) return true;
        e.preventDefault();
        return false;
    }
</script>


On second thoughts, it probably isn't a good idea to put this in the 
standard. 
It is probably within the same realm as spell checking and 
auto-correction. 

cheers,
SDH
Received on Friday, 10 February 2006 14:43:41 UTC

This archive was generated by hypermail 2.3.1 : Monday, 13 April 2015 23:08:26 UTC