Login Retreive lost passwordRegister
Search

Forum Index / Feedback / Autocomplete Emailaddress

[ This topic is solved ]

  Rob Geerts 27 December 07 / 16:31  
Changed at 27 December 07 / 16:36
I just created a new function, based on the 'autocomplete'-function.
'AutoCompleteAfter' can be used to complete an emailaddress for example.
You tell the script after wich character the input has to be auto completed.

When I type: robgeerts@h, the script explodes on @ and checks wich provider match the 'h', in this case, the script wil complete the input with 'otmail.com'

Example:
<?php
$form
->textField("Email","email");
$providers = array ("hotmail.com""live.nl""live.com""gmail.com""yahoo.com""yahoo.nl");
$form->setAutoCompleteAfter("email""@"$providers);
?>


Installation
Add the following function in class.Formhandler.php in the class 'Formhandler'.

<?php
function setAutoCompleteAfter$field$after$options )
{
    static 
$setJS false;

    
// check if the field exists and is a textfield
    
if( !$this->fieldExists($field) || strtolower(get_class$this->_fields[$field][1] )) != 'textfield')
    {
        
trigger_error(
        
'You have to declare the textfield first! '.
        
'The field "'.$field.'" does not exists in the form!',
        
E_USER_WARNING
        
);

        return;
    }

    
// check if the options are correct
    
if( !is_array$options ) )
    {
        
trigger_error'You have to give an array as options!'E_USER_WARNING );
        return;
    }

    
// add the javascript file if not done yet
    
if( !$setJS )
    {
        
$setJS true;
        
$this->_setJSFH_FHTML_DIR.'js/autocomplete.js'true );
    }

    
// create the javascript array
    
$js $field.'_values = [';
    foreach( 
$options as $option )
    {
        
$js .= '"'.htmlentities($option) .'", ';
    }
    
$this->_setJSsubstr($js0, -2)."];\n" );

    
// add the javascript to the fields "extra" argument
    
$this->_fields[$field][1]->_sExtra .= " onkeypress='return autocompleteafter(this, event,\"".$after."\", ".$field."_values);' ";
}
?>


AND

add the following function to js/autocomplete.js:

function autocompleteafter(oTextbox, oEvent, After, arrValues) {
       switch (oEvent.keyCode) {
       case 38: //up arrow
       case 40: //down arrow
       case 37: //left arrow
       case 39: //right arrow
       case 33: //page up
       case 34: //page down
       case 36: //home
       case 35: //end
       case 13: //enter
       case 9: //tab
       case 27: //esc
       case 16: //shift
       case 17: //ctrl
       case 18: //alt
       case 20: //caps lock
       case 8: //backspace
       case 46: //delete
           return true;
           break;

       default:
           textboxReplaceSelect(oTextbox, String.fromCharCode(isIE ? oEvent.keyCode : oEvent.charCode));
           var iLen = oTextbox.value.length;
           var my_string = oTextbox.value;


           var my_array = my_string.split(After);     
           var sMatch = autocompleteMatch(my_array[1], arrValues);
           
           if (sMatch != null) {
               oTextbox.value=my_array[0] + After + sMatch;
               textboxSelect(oTextbox, iLen, oTextbox.value.length);
           }

           return false;
       }
}


AND DONT FORGET:
to replace the function 'autocompleteMatch' (in autocomplete.js) with the function below:

function autocompleteMatch (sText, arrValues) {
    if(sText!=null){
        for (var i=0; i < arrValues.length; i++) {
            if (arrValues[i].toLowerCase().indexOf(sText.toLowerCase()) == 0) {
                return arrValues[i];
            }
        }
    }

    return null;
}



Good Luck!

  Johan Wiegel (Admin) 13 February 08 / 08:01  
this will be added in FH3 v1.2.2

  Rob Geerts 10 March 08 / 17:14  
Thank you for adding this function!

  Top


powered by PHP-GLOBE   © 2004 - 2012 FormHandler. All rights reserved.   -   Open source license