HZGN.COM
welcome to my space
X
Welcome to:hzgn.com
Search:  
NAVIGATION - HOME
Text Field maskRe enhancement
Published by: cfz 2009-01-07

  • Ext.form.TextField.prototype.filterKeys = function(e,f){
    var k = e.getKey();
    if(!Ext.isIE && (e.isNavKeyPress() k == e.BACKSPACE (k == e.DELETE && e.button == -1))){
    return;
    }
    var c = e.getCharCode();
    var s = f.value+String.fromCharCode(c);
    if(!this.maskRe.test(s '')){
    e.stopEvent();
    }
    };


    This code changes the functionality for maskRe instead of verifying that each character matches a regular expression. This verifies that the contents of the text field plus the new character will match a regular expression.

    Example - only allow a valid hostname (label) to be typed.
    maskRe=/^[a-zA-Z]([-.a-zA-Z0-9]{0,61}[a-zA-Z0-9]){0,1}$/

    Maybe this code could be added with a flag to toggle between the character check mode and the entire string check mode?


  • There may be further benefit to allowing more than one mask. It is hard to squeeze it into one check.


    Ext.form.TextField.prototype.maskRe2 = null;

    Ext.form.TextField.prototype.filterKeys = function(e,f){
    var k = e.getKey();
    if(!Ext.isIE && (e.isNavKeyPress() k == e.BACKSPACE (k == e.DELETE && e.button == -1))){
    return;
    }
    var c = e.getCharCode();
    var s = f.value+String.fromCharCode(c);
    if(!this.maskRe.test(s '')){
    e.stopEvent();
    }else if(this.maskRe2 && !this.maskRe2.test(s '')){
    e.stopEvent();
    }
    };


    Might want to nest maskRe3-9.


  • I am trying to get carried away is what I am trying to do. laugh.

    By masking the entire string with the new character I just get a little more control.

    For instance if I wanted to control a user name with the rule that the first chatacter can only be a letter and the remaining characters are a bit more flexible with a maximum length of 15 I could do that with a regular expression.

    /^[a-zA-Z][-_.a-zA-Z0-9]{0,14}$/

    Or if I want to make sure only a number from 0-333 can be entered
    /^([0-9]{1,2})([12][0-9]{2})(3[0-2][0-9])(33[0-3])$/

    And both of these work great with the above code, until you move the cursor to the front of the username and type a number. Or in the case of the port move to the middle of the string 32 and type a 4 making it 342. So technically for this to work the character needs to be inserted into the string at the location marked by the cursor. I do not know if this information is available through Javascript.

    Alternatively, a copy of the string prior to the event could be stored and then this check could be performed on the back end. If it failes the regular expression test then it could be reverted.

    Back to the carried away statement, this is fine and dandy for simple checks where the first character is limited the remaining characters can be something else with a maximum length. Beyond that would take much more thinking. For instance,
    /^[a-zA-Z][-_.a-zA-Z0-9]{3,14}$/
    trying to set a minimum length would prevent you from typing anything. The validation routines are the best place for this.

    So I got ahead of myself when I started this thread. I was thinking I would be able to prevent them from typing an invalid entry at all. But that is impossible as with many strings like an IP until you type out 4 valid octets seperated by 3 dots your string is invalid so the mechanism I described would prevent you from entering the first digit because the total string would be invalid.

    Now, if you really wanted to control the creation of the string so that it grows with proper characters a seperate mask would be needed for each length of the string. Ofcourse at that point regular expressions are probably not the correct way to go.

    So in closing if anyone finds merit in this, the random cursor insertion point need to be handled. The second alternative is pretty simple, but if the first is possible that would be a bit cleaner.

    Cheers! whooaah what a ramble

    EDIT: Yeah this really isn't worth it. I am just going to expand on VTypes


  • Can I ask what you are trying to do? :)


  • Actually, no toggle flag is need.

    Example - this only allows alpha characters
    maskRe=/^[a-zA-Z]{0,}$/


  • Yes, it sounds overly complex. I applaud your decision to abandon it. :D


  • This does not work well if the cursor is not at the end of the string.

    Any idea on how to handle that?





  • Red Hat's Rough Recovery From CFO Exit
    Windows Live Finds a New, Pre-installed Home

    #If you have any other info about this subject , Please add it free.#
    Your name:
    E-mail:
    Telphone:

    Your comments:


    If you have any other info about Text Field maskRe enhancement , Please add it free.
     
    About us |Contact us |Advertisement |Site map |Exchange links
    Copyright© 2008hzgn.com All Rights Reserved